Fixes: NB#217376 - Applauncherd writes log messages, but not to syslog

RevBy: Jussi Lind
pull/1/head
Alexey Shilov 15 years ago
parent 194646aaea
commit 4b6b9888c5

@ -23,7 +23,7 @@ endif(WRT_FOUND)
# Set libraries to be linked. Shared libraries to be preloaded are not linked in anymore, # Set libraries to be linked. Shared libraries to be preloaded are not linked in anymore,
# but dlopen():ed and listed in src/launcher/preload.h instead. # but dlopen():ed and listed in src/launcher/preload.h instead.
link_libraries(${LIBDL} ${QT_QTCORE_LIBRARY}) link_libraries(${LIBDL} )
# Set executable # Set executable
add_library(applauncherd MODULE ${SRC} ${MOC_SRC}) add_library(applauncherd MODULE ${SRC} ${MOC_SRC})

@ -32,6 +32,7 @@
#include <sys/prctl.h> #include <sys/prctl.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <fcntl.h> #include <fcntl.h>
#include <cstring>
#ifdef HAVE_CREDS #ifdef HAVE_CREDS
#include <sys/creds.h> #include <sys/creds.h>

@ -37,6 +37,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <dlfcn.h> #include <dlfcn.h>
#include <glob.h> #include <glob.h>
#include <cstring>
Daemon * Daemon::m_instance = NULL; Daemon * Daemon::m_instance = NULL;
int Daemon::m_lockFd = -1; int Daemon::m_lockFd = -1;

@ -21,88 +21,17 @@
#include <cstdlib> #include <cstdlib>
#include <syslog.h> #include <syslog.h>
#include <cstdarg> #include <cstdarg>
#include <QDateTime> #include <cstdio>
#include <QDir> #include <unistd.h>
#include <QString>
namespace
{
const QString logDirectory("/var/log");
const QString logFileName(logDirectory + QDir::separator() + PROG_NAME_LAUNCHER + ".log");
const QString oldLogFileName(logFileName + ".old");
const QString dateFormat("yyyy-MM-dd hh:mm:ss.zzz");
}
bool Logger::m_isOpened = false; bool Logger::m_isOpened = false;
bool Logger::m_useSyslog = false;
bool Logger::m_echoMode = false; bool Logger::m_echoMode = false;
QTextStream Logger::m_logStream;
QFile Logger::m_logFile;
void Logger::openLog(const char * progName) void Logger::openLog(const char * progName)
{ {
if (!Logger::m_isOpened) openlog(progName, LOG_PID, LOG_DAEMON);
{ Logger::m_isOpened = true;
// Check if it's possible to write under /var/log
// Should make it possible to get the logs in the enviroments
// with and without syslog.
QDir logDir;
if (logDir.exists(logDirectory))
{
// Directory exists, is it possible to create a file in it?
m_logFile.setFileName(oldLogFileName);
if (m_logFile.open(QIODevice::WriteOnly))
{
m_logFile.close();
m_logFile.remove();
}
else
{
// It is not possible to write to file. Use syslog
m_useSyslog = true;
}
}
else
{
// Directory does not exist. Is it possible to create it?
if (logDir.mkdir(logDirectory) == false)
{
// Not possible to create directory. Use syslog.
m_useSyslog = true;
}
}
// Initialize the logging interface
if (m_useSyslog == false)
{
// Remove the oldest log file
m_logFile.setFileName(oldLogFileName);
m_logFile.remove();
// Copy latest log file to .log.old
m_logFile.setFileName(logFileName);
m_logFile.rename(oldLogFileName);
// Open current log file
m_logFile.setFileName(logFileName);
if (m_logFile.open(QIODevice::WriteOnly))
{
Logger::m_logStream.setDevice(&m_logFile);
}
else
{
m_useSyslog = true;
}
}
if (m_useSyslog)
{
openlog(progName, LOG_PID, LOG_DAEMON);
}
Logger::m_isOpened = true;
}
} }
void Logger::closeLog() void Logger::closeLog()
@ -110,16 +39,7 @@ void Logger::closeLog()
if (Logger::m_isOpened) if (Logger::m_isOpened)
{ {
// Close syslog // Close syslog
if (m_useSyslog) closelog();
{
closelog();
}
// Close log file
else
{
m_logFile.close();
}
Logger::m_isOpened = false; Logger::m_isOpened = false;
} }
} }
@ -136,53 +56,10 @@ void Logger::writeLog(const int priority, const char * format, va_list ap)
} }
// Print to syslog // Print to syslog
if (m_useSyslog) vsyslog(priority, format, ap);
{
vsyslog(priority, format, ap);
}
// Print to file
else
{
// Print message to a QString
QString msg;
msg.vsprintf(format, ap);
// Print date and time to the stream
m_logStream <<
QDateTime::currentDateTime().toString(dateFormat);
// Print type prefix to the stream
switch (priority)
{
case LOG_DEBUG:
m_logStream << " [DEBUG] ";
break;
case LOG_ERR:
m_logStream << " [ERROR] ";
break;
case LOG_WARNING:
m_logStream << " [WARNING] ";
break;
case LOG_INFO:
m_logStream << " [INFO] ";
break;
default:
m_logStream << " [N/A] ";
break;
}
// Print message to the stream
m_logStream << msg << "\n";
m_logStream.flush();
}
} }
} }
void Logger::logDebug(const char * format, ...) void Logger::logDebug(const char * format, ...)
{ {
#ifndef DEBUG_LOGGING_DISABLED #ifndef DEBUG_LOGGING_DISABLED
@ -191,7 +68,7 @@ void Logger::logDebug(const char * format, ...)
writeLog(LOG_DEBUG, format, ap); writeLog(LOG_DEBUG, format, ap);
va_end(ap); va_end(ap);
#else #else
Q_UNUSED(format); (void)format;
#endif #endif
} }

@ -20,8 +20,6 @@
#ifndef LOGGER_H #ifndef LOGGER_H
#define LOGGER_H #define LOGGER_H
#include <QTextStream>
#include <QFile>
#include <cstdarg> #include <cstdarg>
/*! /*!
@ -104,13 +102,6 @@ private:
static void writeLog(const int priority, const char * format, va_list ap); static void writeLog(const int priority, const char * format, va_list ap);
//! True if the log is open //! True if the log is open
static bool m_isOpened; static bool m_isOpened;
//! Log file which is used if the syslog is not available
//static QFile m_logFile;
static QFile m_logFile;
//! Text stream for log file
static QTextStream m_logStream;
//! True if the syslog is available
static bool m_useSyslog;
//! Echo everything to stdout if true //! Echo everything to stdout if true
static bool m_echoMode; static bool m_echoMode;
}; };

@ -26,7 +26,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/file.h> #include <sys/file.h>
#include <QtCore/QtGlobal> #define DECL_EXPORT extern "C" __attribute__ ((__visibility__("default")))
//! Signal handler to reap zombie processes //! Signal handler to reap zombie processes
void reapZombies(int) void reapZombies(int)
@ -43,7 +43,7 @@ void exitLauncher(int)
} }
//! Main function //! Main function
Q_DECL_EXPORT int main(int argc, char * argv[]) DECL_EXPORT int main(int argc, char * argv[])
{ {
// Open the log // Open the log

@ -24,6 +24,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <cstdlib> #include <cstdlib>
#include <cstring>
void SocketManager::initSocket(const string & socketId) void SocketManager::initSocket(const string & socketId)
{ {

Loading…
Cancel
Save