2011-06-21 17:19:10 +08:00
|
|
|
/*
|
|
|
|
* ZoneMinder Logger Implementation, $Date$, $Revision$
|
|
|
|
* Copyright (C) 2001-2008 Philip Coombes
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2016-12-26 23:23:16 +08:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2011-06-21 17:19:10 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "zm_logger.h"
|
|
|
|
|
2017-07-07 05:45:23 +08:00
|
|
|
#include "zm_db.h"
|
2021-02-04 11:47:28 +08:00
|
|
|
#include "zm_utils.h"
|
2021-02-26 01:26:26 +08:00
|
|
|
|
2017-01-31 16:10:25 +08:00
|
|
|
#include <libgen.h>
|
2021-02-04 11:47:28 +08:00
|
|
|
#include <syslog.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2015-02-24 22:22:08 +08:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#include <sys/thr.h>
|
|
|
|
#endif
|
2021-02-26 01:26:26 +08:00
|
|
|
#include <cerrno>
|
|
|
|
#include <csignal>
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstring>
|
|
|
|
|
2011-06-21 17:19:10 +08:00
|
|
|
|
|
|
|
bool Logger::smInitialised = false;
|
2020-08-26 07:45:48 +08:00
|
|
|
Logger *Logger::smInstance = nullptr;
|
2011-06-21 17:19:10 +08:00
|
|
|
|
|
|
|
Logger::StringMap Logger::smCodes;
|
|
|
|
Logger::IntMap Logger::smSyslogPriorities;
|
|
|
|
|
2019-09-17 23:10:59 +08:00
|
|
|
void Logger::usrHandler(int sig) {
|
2016-04-04 22:11:48 +08:00
|
|
|
Logger *logger = fetch();
|
|
|
|
if ( sig == SIGUSR1 )
|
2018-06-09 03:21:43 +08:00
|
|
|
logger->level(logger->level()+1);
|
2016-04-04 22:11:48 +08:00
|
|
|
else if ( sig == SIGUSR2 )
|
2018-06-09 03:21:43 +08:00
|
|
|
logger->level(logger->level()-1);
|
2018-03-22 22:25:56 +08:00
|
|
|
Info("Logger - Level changed to %d", logger->level());
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Logger::Logger() :
|
2018-03-22 22:25:56 +08:00
|
|
|
mLevel(INFO),
|
|
|
|
mTerminalLevel(NOLOG),
|
|
|
|
mDatabaseLevel(NOLOG),
|
|
|
|
mFileLevel(NOLOG),
|
|
|
|
mSyslogLevel(NOLOG),
|
|
|
|
mEffectiveLevel(NOLOG),
|
|
|
|
mDbConnected(false),
|
2019-09-17 23:10:59 +08:00
|
|
|
mLogPath(staticConfig.PATH_LOGS.c_str()),
|
2021-02-26 01:26:26 +08:00
|
|
|
// mLogFile( mLogPath+"/"+mId+".log" ),
|
2020-08-26 07:45:48 +08:00
|
|
|
mLogFileFP(nullptr),
|
2018-03-22 22:25:56 +08:00
|
|
|
mHasTerminal(false),
|
|
|
|
mFlush(false) {
|
2021-02-26 01:26:26 +08:00
|
|
|
if (smInstance) {
|
2018-06-09 03:21:43 +08:00
|
|
|
Panic("Attempt to create second instance of Logger class");
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
|
|
|
|
2021-02-26 01:26:26 +08:00
|
|
|
if (!smInitialised) {
|
2016-04-04 22:11:48 +08:00
|
|
|
smCodes[INFO] = "INF";
|
|
|
|
smCodes[WARNING] = "WAR";
|
|
|
|
smCodes[ERROR] = "ERR";
|
|
|
|
smCodes[FATAL] = "FAT";
|
|
|
|
smCodes[PANIC] = "PNC";
|
|
|
|
smCodes[NOLOG] = "OFF";
|
|
|
|
|
|
|
|
smSyslogPriorities[INFO] = LOG_INFO;
|
|
|
|
smSyslogPriorities[WARNING] = LOG_WARNING;
|
|
|
|
smSyslogPriorities[ERROR] = LOG_ERR;
|
|
|
|
smSyslogPriorities[FATAL] = LOG_ERR;
|
|
|
|
smSyslogPriorities[PANIC] = LOG_ERR;
|
|
|
|
|
|
|
|
char code[4] = "";
|
2021-02-26 01:26:26 +08:00
|
|
|
for (int i = DEBUG1; i <= DEBUG9; i++) {
|
2018-03-30 00:24:00 +08:00
|
|
|
snprintf(code, sizeof(code), "DB%d", i);
|
2016-04-04 22:11:48 +08:00
|
|
|
smCodes[i] = code;
|
|
|
|
smSyslogPriorities[i] = LOG_DEBUG;
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
smInitialised = true;
|
2021-02-26 01:26:26 +08:00
|
|
|
} // end if ! smInitialised
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2021-02-26 01:26:26 +08:00
|
|
|
if (fileno(stderr) && isatty(fileno(stderr))) {
|
2018-02-18 01:25:00 +08:00
|
|
|
mHasTerminal = true;
|
2019-09-17 23:10:59 +08:00
|
|
|
mTerminalLevel = WARNING;
|
|
|
|
}
|
|
|
|
} // End Logger::Logger
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2017-05-20 00:57:43 +08:00
|
|
|
Logger::~Logger() {
|
2016-04-04 22:11:48 +08:00
|
|
|
terminate();
|
2017-09-24 01:41:20 +08:00
|
|
|
smCodes.clear();
|
|
|
|
smSyslogPriorities.clear();
|
2018-03-30 00:24:00 +08:00
|
|
|
smInitialised = false;
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2018-03-22 22:25:56 +08:00
|
|
|
void Logger::initialise(const std::string &id, const Options &options) {
|
2016-04-04 22:11:48 +08:00
|
|
|
char *envPtr;
|
|
|
|
|
|
|
|
if ( !id.empty() )
|
2018-02-15 02:50:16 +08:00
|
|
|
this->id(id);
|
2016-04-04 22:11:48 +08:00
|
|
|
|
|
|
|
std::string tempLogFile;
|
2017-10-19 01:22:15 +08:00
|
|
|
|
2018-06-09 03:21:43 +08:00
|
|
|
if ( (envPtr = getTargettedEnv("LOG_FILE")) ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
tempLogFile = envPtr;
|
2018-06-09 03:21:43 +08:00
|
|
|
} else if ( options.mLogFile.size() ) {
|
2017-10-19 01:22:15 +08:00
|
|
|
tempLogFile = options.mLogFile;
|
2018-06-09 03:21:43 +08:00
|
|
|
} else {
|
2019-09-17 23:10:59 +08:00
|
|
|
// options.mLogPath defaults to '.' so only use it if we don't already have a path
|
|
|
|
if ( (!mLogPath.size()) || options.mLogPath != "." ) {
|
2017-10-19 01:22:15 +08:00
|
|
|
mLogPath = options.mLogPath;
|
|
|
|
}
|
|
|
|
tempLogFile = mLogPath+"/"+mId+".log";
|
|
|
|
}
|
2016-04-04 22:11:48 +08:00
|
|
|
|
|
|
|
Level tempLevel = INFO;
|
2018-02-13 19:23:18 +08:00
|
|
|
Level tempTerminalLevel = mTerminalLevel;
|
2016-04-04 22:11:48 +08:00
|
|
|
Level tempDatabaseLevel = mDatabaseLevel;
|
|
|
|
Level tempFileLevel = mFileLevel;
|
|
|
|
Level tempSyslogLevel = mSyslogLevel;
|
|
|
|
|
2018-02-13 19:23:18 +08:00
|
|
|
if ( options.mTerminalLevel != NOOPT )
|
|
|
|
tempTerminalLevel = options.mTerminalLevel;
|
2018-02-15 02:50:16 +08:00
|
|
|
|
2020-01-11 05:42:18 +08:00
|
|
|
// DEBUG1 == 1. So >= DEBUG1, we set to DEBUG9?! Why?
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( options.mDatabaseLevel != NOOPT )
|
|
|
|
tempDatabaseLevel = options.mDatabaseLevel;
|
|
|
|
else
|
|
|
|
tempDatabaseLevel = config.log_level_database >= DEBUG1 ? DEBUG9 : config.log_level_database;
|
2018-02-15 02:50:16 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( options.mFileLevel != NOOPT )
|
|
|
|
tempFileLevel = options.mFileLevel;
|
|
|
|
else
|
|
|
|
tempFileLevel = config.log_level_file >= DEBUG1 ? DEBUG9 : config.log_level_file;
|
2018-02-15 02:50:16 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( options.mSyslogLevel != NOOPT )
|
|
|
|
tempSyslogLevel = options.mSyslogLevel;
|
|
|
|
else
|
|
|
|
tempSyslogLevel = config.log_level_syslog >= DEBUG1 ? DEBUG9 : config.log_level_syslog;
|
|
|
|
|
|
|
|
// Legacy
|
2018-06-09 03:21:43 +08:00
|
|
|
if ( (envPtr = getenv("LOG_PRINT")) )
|
2018-02-13 19:23:18 +08:00
|
|
|
tempTerminalLevel = atoi(envPtr) ? DEBUG9 : NOLOG;
|
2016-04-04 22:11:48 +08:00
|
|
|
|
2018-03-22 22:25:56 +08:00
|
|
|
if ( (envPtr = getTargettedEnv("LOG_LEVEL")) )
|
2016-04-04 22:11:48 +08:00
|
|
|
tempLevel = atoi(envPtr);
|
|
|
|
|
2018-03-22 22:25:56 +08:00
|
|
|
if ( (envPtr = getTargettedEnv("LOG_LEVEL_TERM")) )
|
2018-02-13 19:23:18 +08:00
|
|
|
tempTerminalLevel = atoi(envPtr);
|
2018-03-22 22:25:56 +08:00
|
|
|
if ( (envPtr = getTargettedEnv("LOG_LEVEL_DATABASE")) )
|
2016-04-04 22:11:48 +08:00
|
|
|
tempDatabaseLevel = atoi(envPtr);
|
2018-03-22 22:25:56 +08:00
|
|
|
if ( (envPtr = getTargettedEnv("LOG_LEVEL_FILE")) )
|
2016-04-04 22:11:48 +08:00
|
|
|
tempFileLevel = atoi(envPtr);
|
2018-03-22 22:25:56 +08:00
|
|
|
if ( (envPtr = getTargettedEnv("LOG_LEVEL_SYSLOG")) )
|
2016-04-04 22:11:48 +08:00
|
|
|
tempSyslogLevel = atoi(envPtr);
|
|
|
|
|
2017-05-20 00:57:43 +08:00
|
|
|
if ( config.log_debug ) {
|
2018-03-22 22:25:56 +08:00
|
|
|
StringVector targets = split(config.log_debug_target, "|");
|
2017-05-20 00:57:43 +08:00
|
|
|
for ( unsigned int i = 0; i < targets.size(); i++ ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
const std::string &target = targets[i];
|
2017-11-13 02:16:40 +08:00
|
|
|
if ( target == mId || target == "_"+mId || target == "_"+mIdRoot || target == "" ) {
|
2017-05-20 00:57:43 +08:00
|
|
|
if ( config.log_debug_level > NOLOG ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
tempLevel = config.log_debug_level;
|
2017-05-20 00:57:43 +08:00
|
|
|
if ( config.log_debug_file[0] ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
tempLogFile = config.log_debug_file;
|
|
|
|
tempFileLevel = tempLevel;
|
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2021-02-26 01:26:26 +08:00
|
|
|
} // end foreach target
|
2017-09-24 01:41:20 +08:00
|
|
|
} else {
|
|
|
|
// if we don't have debug turned on, then the max effective log level is INFO
|
|
|
|
if ( tempSyslogLevel > INFO ) tempSyslogLevel = INFO;
|
|
|
|
if ( tempFileLevel > INFO ) tempFileLevel = INFO;
|
2018-02-13 19:23:18 +08:00
|
|
|
if ( tempTerminalLevel > INFO ) tempTerminalLevel = INFO;
|
2017-09-24 01:41:20 +08:00
|
|
|
if ( tempDatabaseLevel > INFO ) tempDatabaseLevel = INFO;
|
|
|
|
if ( tempLevel > INFO ) tempLevel = INFO;
|
2021-02-26 01:26:26 +08:00
|
|
|
} // end if config.log_debug
|
2017-05-20 20:53:27 +08:00
|
|
|
|
2018-03-22 22:25:56 +08:00
|
|
|
logFile(tempLogFile);
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2018-03-22 22:25:56 +08:00
|
|
|
terminalLevel(tempTerminalLevel);
|
|
|
|
databaseLevel(tempDatabaseLevel);
|
|
|
|
fileLevel(tempFileLevel);
|
|
|
|
syslogLevel(tempSyslogLevel);
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2018-03-22 22:25:56 +08:00
|
|
|
level(tempLevel);
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2017-06-09 23:05:44 +08:00
|
|
|
mFlush = false;
|
2017-06-30 03:41:21 +08:00
|
|
|
if ( (envPtr = getenv("LOG_FLUSH")) ) {
|
2018-06-09 03:21:43 +08:00
|
|
|
mFlush = atoi(envPtr);
|
2017-06-09 23:05:44 +08:00
|
|
|
} else if ( config.log_debug ) {
|
|
|
|
mFlush = true;
|
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
{
|
|
|
|
struct sigaction action;
|
2018-03-22 22:25:56 +08:00
|
|
|
memset(&action, 0, sizeof(action));
|
2016-04-04 22:11:48 +08:00
|
|
|
action.sa_handler = usrHandler;
|
|
|
|
action.sa_flags = SA_RESTART;
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2018-03-22 22:25:56 +08:00
|
|
|
// Does this REALLY need to be fatal?
|
|
|
|
if ( sigaction(SIGUSR1, &action, 0) < 0 ) {
|
|
|
|
Fatal("sigaction(), error = %s", strerror(errno));
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2018-03-22 22:25:56 +08:00
|
|
|
if ( sigaction(SIGUSR2, &action, 0) < 0) {
|
|
|
|
Fatal("sigaction(), error = %s", strerror(errno));
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
mInitialised = true;
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2019-09-17 23:36:57 +08:00
|
|
|
Debug(1, "LogOpts: level=%s effective=%s, screen=%s, database=%s, logfile=%s->%s, syslog=%s",
|
2017-05-20 00:57:43 +08:00
|
|
|
smCodes[mLevel].c_str(),
|
|
|
|
smCodes[mEffectiveLevel].c_str(),
|
2018-02-13 19:23:18 +08:00
|
|
|
smCodes[mTerminalLevel].c_str(),
|
2017-05-20 00:57:43 +08:00
|
|
|
smCodes[mDatabaseLevel].c_str(),
|
|
|
|
smCodes[mFileLevel].c_str(),
|
|
|
|
mLogFile.c_str(),
|
|
|
|
smCodes[mSyslogLevel].c_str()
|
|
|
|
);
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2017-05-20 00:57:43 +08:00
|
|
|
void Logger::terminate() {
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( mFileLevel > NOLOG )
|
|
|
|
closeFile();
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( mSyslogLevel > NOLOG )
|
|
|
|
closeSyslog();
|
2014-11-15 05:17:44 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( mDatabaseLevel > NOLOG )
|
|
|
|
closeDatabase();
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2018-03-22 22:25:56 +08:00
|
|
|
// These don't belong here, they have nothing to do with logging
|
|
|
|
bool Logger::boolEnv(const std::string &name, bool defaultValue) {
|
|
|
|
const char *envPtr = getenv(name.c_str());
|
|
|
|
return envPtr ? atoi(envPtr) : defaultValue;
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2018-03-22 22:25:56 +08:00
|
|
|
int Logger::intEnv(const std::string &name, bool defaultValue) {
|
|
|
|
const char *envPtr = getenv(name.c_str());
|
|
|
|
return envPtr ? atoi(envPtr) : defaultValue;
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2018-03-22 22:25:56 +08:00
|
|
|
std::string Logger::strEnv(const std::string &name, const std::string &defaultValue) {
|
|
|
|
const char *envPtr = getenv(name.c_str());
|
|
|
|
return envPtr ? envPtr : defaultValue;
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2018-03-22 22:25:56 +08:00
|
|
|
char *Logger::getTargettedEnv(const std::string &name) {
|
2017-11-13 02:16:40 +08:00
|
|
|
std::string envName = name+"_"+mId;
|
2018-03-22 22:25:56 +08:00
|
|
|
char *envPtr = getenv(envName.c_str());
|
2017-05-20 00:57:43 +08:00
|
|
|
if ( !envPtr && mId != mIdRoot ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
envName = name+"_"+mIdRoot;
|
2018-03-22 22:25:56 +08:00
|
|
|
envPtr = getenv(envName.c_str());
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
|
|
|
if ( !envPtr )
|
2018-03-22 22:25:56 +08:00
|
|
|
envPtr = getenv(name.c_str());
|
2017-11-13 02:16:40 +08:00
|
|
|
return envPtr;
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2018-03-22 22:25:56 +08:00
|
|
|
const std::string &Logger::id(const std::string &id) {
|
2016-04-04 22:11:48 +08:00
|
|
|
std::string tempId = id;
|
|
|
|
|
|
|
|
size_t pos;
|
|
|
|
// Remove whitespace
|
2019-09-17 23:10:59 +08:00
|
|
|
while ( (pos = tempId.find_first_of(" \t")) != std::string::npos ) {
|
2018-03-22 22:25:56 +08:00
|
|
|
tempId.replace(pos, 1, "");
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
|
|
|
// Replace non-alphanum with underscore
|
2018-03-22 22:25:56 +08:00
|
|
|
while ( (pos = tempId.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_")) != std::string::npos ) {
|
|
|
|
tempId.replace(pos, 1, "_");
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2017-05-20 00:57:43 +08:00
|
|
|
if ( mId != tempId ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
mId = tempId;
|
2018-03-22 22:25:56 +08:00
|
|
|
pos = mId.find('_');
|
2017-05-20 00:57:43 +08:00
|
|
|
if ( pos != std::string::npos ) {
|
2018-03-22 22:25:56 +08:00
|
|
|
mIdRoot = mId.substr(0, pos);
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( ++pos < mId.size() )
|
2018-03-22 22:25:56 +08:00
|
|
|
mIdArgs = mId.substr(pos);
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2018-03-30 00:24:00 +08:00
|
|
|
return mId;
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2018-03-22 22:25:56 +08:00
|
|
|
Logger::Level Logger::level(Logger::Level level) {
|
2017-05-20 00:57:43 +08:00
|
|
|
if ( level > NOOPT ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
level = limit(level);
|
|
|
|
if ( mLevel != level )
|
|
|
|
mLevel = level;
|
|
|
|
|
|
|
|
mEffectiveLevel = NOLOG;
|
2018-02-13 19:23:18 +08:00
|
|
|
if ( mTerminalLevel > mEffectiveLevel )
|
|
|
|
mEffectiveLevel = mTerminalLevel;
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( mDatabaseLevel > mEffectiveLevel )
|
|
|
|
mEffectiveLevel = mDatabaseLevel;
|
|
|
|
if ( mFileLevel > mEffectiveLevel )
|
|
|
|
mEffectiveLevel = mFileLevel;
|
|
|
|
if ( mSyslogLevel > mEffectiveLevel )
|
|
|
|
mEffectiveLevel = mSyslogLevel;
|
|
|
|
if ( mEffectiveLevel > mLevel)
|
|
|
|
mEffectiveLevel = mLevel;
|
2018-09-25 02:17:16 +08:00
|
|
|
|
|
|
|
// DEBUG levels should flush
|
|
|
|
if ( mLevel > INFO )
|
|
|
|
mFlush = true;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2018-03-30 00:24:00 +08:00
|
|
|
return mLevel;
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2019-09-17 23:10:59 +08:00
|
|
|
Logger::Level Logger::terminalLevel(Logger::Level terminalLevel) {
|
2018-02-18 01:25:00 +08:00
|
|
|
if ( terminalLevel > NOOPT ) {
|
|
|
|
if ( !mHasTerminal )
|
|
|
|
terminalLevel = NOLOG;
|
|
|
|
terminalLevel = limit(terminalLevel);
|
|
|
|
if ( mTerminalLevel != terminalLevel )
|
|
|
|
mTerminalLevel = terminalLevel;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2018-03-30 00:24:00 +08:00
|
|
|
return mTerminalLevel;
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2019-09-17 23:10:59 +08:00
|
|
|
Logger::Level Logger::databaseLevel(Logger::Level databaseLevel) {
|
2021-02-07 20:55:08 +08:00
|
|
|
if (databaseLevel > NOOPT) {
|
2016-04-04 22:11:48 +08:00
|
|
|
databaseLevel = limit(databaseLevel);
|
2021-02-07 20:55:08 +08:00
|
|
|
if (mDatabaseLevel != databaseLevel) {
|
|
|
|
if ((databaseLevel > NOLOG) && (mDatabaseLevel <= NOLOG)) { // <= NOLOG would be NOOPT
|
|
|
|
if (!zmDbConnected) {
|
2019-09-17 23:10:59 +08:00
|
|
|
databaseLevel = NOLOG;
|
|
|
|
}
|
2021-02-07 20:55:08 +08:00
|
|
|
}
|
2016-04-04 22:11:48 +08:00
|
|
|
mDatabaseLevel = databaseLevel;
|
2021-02-07 20:55:08 +08:00
|
|
|
}
|
|
|
|
}
|
2017-05-20 00:57:43 +08:00
|
|
|
|
2018-03-30 00:24:00 +08:00
|
|
|
return mDatabaseLevel;
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2019-09-17 23:10:59 +08:00
|
|
|
Logger::Level Logger::fileLevel(Logger::Level fileLevel) {
|
2021-02-26 01:26:26 +08:00
|
|
|
if (fileLevel > NOOPT) {
|
2016-04-04 22:11:48 +08:00
|
|
|
fileLevel = limit(fileLevel);
|
2018-02-15 02:50:16 +08:00
|
|
|
// Always close, because we may have changed file names
|
2021-02-26 01:26:26 +08:00
|
|
|
if (mFileLevel > NOLOG)
|
|
|
|
closeFile();
|
2018-02-15 02:50:16 +08:00
|
|
|
mFileLevel = fileLevel;
|
2019-09-17 23:36:57 +08:00
|
|
|
// Don't try to open it here because it will create the log file even if we never write to it.
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2018-03-30 00:24:00 +08:00
|
|
|
return mFileLevel;
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2019-09-17 23:10:59 +08:00
|
|
|
Logger::Level Logger::syslogLevel(Logger::Level syslogLevel) {
|
2021-02-26 01:26:26 +08:00
|
|
|
if (syslogLevel > NOOPT) {
|
2016-04-04 22:11:48 +08:00
|
|
|
syslogLevel = limit(syslogLevel);
|
2021-02-26 01:26:26 +08:00
|
|
|
if (mSyslogLevel != syslogLevel) {
|
|
|
|
if (mSyslogLevel > NOLOG)
|
2016-04-04 22:11:48 +08:00
|
|
|
closeSyslog();
|
|
|
|
mSyslogLevel = syslogLevel;
|
2021-02-26 01:26:26 +08:00
|
|
|
if (mSyslogLevel > NOLOG)
|
2016-04-04 22:11:48 +08:00
|
|
|
openSyslog();
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2018-03-30 00:24:00 +08:00
|
|
|
return mSyslogLevel;
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2019-09-17 23:10:59 +08:00
|
|
|
void Logger::logFile(const std::string &logFile) {
|
2016-04-04 22:11:48 +08:00
|
|
|
bool addLogPid = false;
|
|
|
|
std::string tempLogFile = logFile;
|
2021-02-26 01:26:26 +08:00
|
|
|
if (tempLogFile[tempLogFile.length()-1] == '+') {
|
2016-04-04 22:11:48 +08:00
|
|
|
tempLogFile.resize(tempLogFile.length()-1);
|
|
|
|
addLogPid = true;
|
|
|
|
}
|
2021-02-26 01:26:26 +08:00
|
|
|
if (addLogPid)
|
2019-09-17 23:10:59 +08:00
|
|
|
mLogFile = stringtf("%s.%05d", tempLogFile.c_str(), getpid());
|
2016-04-04 22:11:48 +08:00
|
|
|
else
|
|
|
|
mLogFile = tempLogFile;
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2017-05-20 00:57:43 +08:00
|
|
|
void Logger::openFile() {
|
2021-02-26 01:26:26 +08:00
|
|
|
if (mLogFile.size()) {
|
|
|
|
if ( (mLogFileFP = fopen(mLogFile.c_str(), "a")) == nullptr ) {
|
|
|
|
mFileLevel = NOLOG;
|
|
|
|
Error("fopen() for %s, error = %s", mLogFile.c_str(), strerror(errno));
|
|
|
|
}
|
2018-02-15 00:56:54 +08:00
|
|
|
} else {
|
|
|
|
puts("Called Logger::openFile() without a filename");
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2017-05-20 00:57:43 +08:00
|
|
|
void Logger::closeFile() {
|
2021-02-26 01:26:26 +08:00
|
|
|
if (mLogFileFP) {
|
2019-09-17 23:10:59 +08:00
|
|
|
fflush(mLogFileFP);
|
2021-02-26 01:26:26 +08:00
|
|
|
if (fclose(mLogFileFP) < 0) {
|
2020-08-26 07:45:48 +08:00
|
|
|
mLogFileFP = nullptr;
|
2019-09-17 23:10:59 +08:00
|
|
|
Error("fclose(), error = %s", strerror(errno));
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
2020-08-26 07:45:48 +08:00
|
|
|
mLogFileFP = nullptr;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2017-05-20 00:57:43 +08:00
|
|
|
void Logger::closeDatabase() {
|
2014-11-15 05:17:44 +08:00
|
|
|
}
|
|
|
|
|
2017-05-20 00:57:43 +08:00
|
|
|
void Logger::openSyslog() {
|
2019-09-17 23:10:59 +08:00
|
|
|
(void) openlog(mId.c_str(), LOG_PID|LOG_NDELAY, LOG_LOCAL1);
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2017-05-20 00:57:43 +08:00
|
|
|
void Logger::closeSyslog() {
|
2016-04-04 22:11:48 +08:00
|
|
|
(void) closelog();
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2019-09-17 23:10:59 +08:00
|
|
|
void Logger::logPrint(bool hex, const char * const filepath, const int line, const int level, const char *fstring, ...) {
|
2021-02-26 01:26:26 +08:00
|
|
|
if (level > mEffectiveLevel) return;
|
|
|
|
if (level < PANIC || level > DEBUG9)
|
|
|
|
Panic("Invalid logger level %d", level);
|
2019-09-17 23:10:59 +08:00
|
|
|
|
2018-11-24 01:45:41 +08:00
|
|
|
log_mutex.lock();
|
2021-02-26 01:26:26 +08:00
|
|
|
// Can we save some cycles by having these as members and not allocate them on the fly? I think so.
|
2017-08-12 00:03:37 +08:00
|
|
|
char timeString[64];
|
|
|
|
char logString[8192];
|
|
|
|
va_list argPtr;
|
|
|
|
struct timeval timeVal;
|
2015-08-22 07:28:50 +08:00
|
|
|
|
2021-02-25 01:23:02 +08:00
|
|
|
const char *base = strrchr(filepath, '/');
|
|
|
|
const char *file = base ? base+1 : filepath;
|
2017-08-12 00:03:37 +08:00
|
|
|
const char *classString = smCodes[level].c_str();
|
2017-01-31 16:10:25 +08:00
|
|
|
|
2020-08-26 07:45:48 +08:00
|
|
|
gettimeofday(&timeVal, nullptr);
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2017-05-20 00:57:43 +08:00
|
|
|
#if 0
|
2017-08-12 00:03:37 +08:00
|
|
|
if ( logRuntime ) {
|
|
|
|
static struct timeval logStart;
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2017-08-12 00:03:37 +08:00
|
|
|
subtractTime( &timeVal, &logStart );
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2017-08-12 00:03:37 +08:00
|
|
|
snprintf( timeString, sizeof(timeString), "%ld.%03ld", timeVal.tv_sec, timeVal.tv_usec/1000 );
|
|
|
|
} else {
|
2017-05-20 00:57:43 +08:00
|
|
|
#endif
|
2017-08-12 00:03:37 +08:00
|
|
|
char *timePtr = timeString;
|
2018-03-30 00:24:00 +08:00
|
|
|
timePtr += strftime(timePtr, sizeof(timeString), "%x %H:%M:%S", localtime(&timeVal.tv_sec));
|
|
|
|
snprintf(timePtr, sizeof(timeString)-(timePtr-timeString), ".%06ld", timeVal.tv_usec);
|
2017-05-20 00:57:43 +08:00
|
|
|
#if 0
|
2017-08-12 00:03:37 +08:00
|
|
|
}
|
2017-05-20 00:57:43 +08:00
|
|
|
#endif
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2017-08-12 00:03:37 +08:00
|
|
|
pid_t tid;
|
2015-02-24 22:22:08 +08:00
|
|
|
#ifdef __FreeBSD__
|
2017-08-12 00:03:37 +08:00
|
|
|
long lwpid;
|
|
|
|
thr_self(&lwpid);
|
|
|
|
tid = lwpid;
|
2015-02-24 22:22:08 +08:00
|
|
|
|
2019-09-17 23:10:59 +08:00
|
|
|
if ( tid < 0 ) // Thread/Process id
|
2015-02-24 22:22:08 +08:00
|
|
|
#else
|
2019-09-17 23:10:59 +08:00
|
|
|
#ifdef HAVE_SYSCALL
|
|
|
|
#ifdef __FreeBSD_kernel__
|
2021-02-26 01:26:26 +08:00
|
|
|
if ((syscall(SYS_thr_self, &tid)) < 0) // Thread/Process id
|
2017-05-20 00:57:43 +08:00
|
|
|
|
2019-09-17 23:10:59 +08:00
|
|
|
# else
|
2017-08-12 00:03:37 +08:00
|
|
|
// SOLARIS doesn't have SYS_gettid; don't assume
|
2019-09-17 23:10:59 +08:00
|
|
|
#ifdef SYS_gettid
|
2021-02-26 01:26:26 +08:00
|
|
|
if ((tid = syscall(SYS_gettid)) < 0) // Thread/Process id
|
2019-09-17 23:10:59 +08:00
|
|
|
#endif // SYS_gettid
|
|
|
|
#endif
|
|
|
|
#endif // HAVE_SYSCALL
|
2015-02-24 22:22:08 +08:00
|
|
|
#endif
|
2019-09-17 23:10:59 +08:00
|
|
|
tid = getpid(); // Process id
|
2017-08-12 00:03:37 +08:00
|
|
|
|
|
|
|
char *logPtr = logString;
|
2019-09-17 23:10:59 +08:00
|
|
|
logPtr += snprintf(logPtr, sizeof(logString), "%s %s[%d].%s-%s/%d [",
|
2017-08-12 00:03:37 +08:00
|
|
|
timeString,
|
|
|
|
mId.c_str(),
|
|
|
|
tid,
|
|
|
|
classString,
|
|
|
|
file,
|
|
|
|
line
|
|
|
|
);
|
|
|
|
char *syslogStart = logPtr;
|
|
|
|
|
2018-08-18 00:57:27 +08:00
|
|
|
va_start(argPtr, fstring);
|
2017-08-12 00:03:37 +08:00
|
|
|
if ( hex ) {
|
2018-08-18 00:57:27 +08:00
|
|
|
unsigned char *data = va_arg(argPtr, unsigned char *);
|
|
|
|
int len = va_arg(argPtr, int);
|
2017-08-12 00:03:37 +08:00
|
|
|
int i;
|
2018-08-18 00:57:27 +08:00
|
|
|
logPtr += snprintf(logPtr, sizeof(logString)-(logPtr-logString), "%d:", len);
|
2017-08-12 00:03:37 +08:00
|
|
|
for ( i = 0; i < len; i++ ) {
|
2018-08-18 00:57:27 +08:00
|
|
|
logPtr += snprintf(logPtr, sizeof(logString)-(logPtr-logString), " %02x", data[i]);
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2017-08-12 00:03:37 +08:00
|
|
|
} else {
|
2018-08-18 00:57:27 +08:00
|
|
|
logPtr += vsnprintf(logPtr, sizeof(logString)-(logPtr-logString), fstring, argPtr);
|
2017-08-12 00:03:37 +08:00
|
|
|
}
|
|
|
|
va_end(argPtr);
|
|
|
|
char *syslogEnd = logPtr;
|
2019-09-17 23:10:59 +08:00
|
|
|
strncpy(logPtr, "]\n", sizeof(logString)-(logPtr-logString));
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2021-02-26 01:26:26 +08:00
|
|
|
if (level <= mTerminalLevel) {
|
2018-08-18 04:15:30 +08:00
|
|
|
puts(logString);
|
|
|
|
fflush(stdout);
|
2017-08-12 00:03:37 +08:00
|
|
|
}
|
2020-12-31 22:14:03 +08:00
|
|
|
|
2021-02-26 01:26:26 +08:00
|
|
|
if (level <= mFileLevel) {
|
|
|
|
if (!mLogFileFP) {
|
|
|
|
// FIXME unlocking here is a problem. Another thread could sneak in.
|
2020-03-05 03:14:28 +08:00
|
|
|
log_mutex.unlock();
|
2021-02-26 01:26:26 +08:00
|
|
|
// We do this here so that we only create the file if we ever write to it.
|
2019-09-17 23:36:57 +08:00
|
|
|
openFile();
|
2020-03-05 03:14:28 +08:00
|
|
|
log_mutex.lock();
|
|
|
|
}
|
2021-02-26 01:26:26 +08:00
|
|
|
if (mLogFileFP) {
|
2018-08-18 04:15:30 +08:00
|
|
|
fputs(logString, mLogFileFP);
|
2021-02-26 01:26:26 +08:00
|
|
|
if (mFlush) fflush(mLogFileFP);
|
2017-08-12 00:03:37 +08:00
|
|
|
} else {
|
2019-09-17 23:36:57 +08:00
|
|
|
puts("Logging to file, but failed to open it\n");
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2020-12-31 22:14:03 +08:00
|
|
|
} // end if level <= mFileLevel
|
2017-08-12 00:03:37 +08:00
|
|
|
|
2021-02-26 01:26:26 +08:00
|
|
|
if (level <= mDatabaseLevel) {
|
|
|
|
if (zmDbConnected) {
|
|
|
|
int syslogSize = syslogEnd-syslogStart;
|
|
|
|
char escapedString[(syslogSize*2)+1];
|
|
|
|
mysql_real_escape_string(&dbconn, escapedString, syslogStart, syslogSize);
|
|
|
|
|
|
|
|
std::string sql_string = stringtf(
|
|
|
|
"INSERT INTO `Logs` "
|
|
|
|
"( `TimeKey`, `Component`, `ServerId`, `Pid`, `Level`, `Code`, `Message`, `File`, `Line` )"
|
|
|
|
" VALUES "
|
|
|
|
"( %ld.%06ld, '%s', %d, %d, %d, '%s', '%s', '%s', %d )",
|
|
|
|
timeVal.tv_sec, timeVal.tv_usec, mId.c_str(), staticConfig.SERVER_ID, tid, level, classString, escapedString, file, line
|
|
|
|
);
|
2021-02-26 06:08:43 +08:00
|
|
|
dbQueue.push(std::move(sql_string));
|
2021-02-26 01:26:26 +08:00
|
|
|
} else {
|
|
|
|
puts("Db is closed");
|
|
|
|
}
|
2020-12-31 22:14:03 +08:00
|
|
|
} // end if level <= mDatabaseLevel
|
|
|
|
|
2021-02-26 01:26:26 +08:00
|
|
|
if (level <= mSyslogLevel) {
|
2020-12-31 22:14:03 +08:00
|
|
|
*syslogEnd = '\0';
|
|
|
|
syslog(smSyslogPriorities[level], "%s [%s] [%s]", classString, mId.c_str(), syslogStart);
|
2017-08-12 00:03:37 +08:00
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2020-12-31 22:14:03 +08:00
|
|
|
log_mutex.unlock();
|
2021-02-26 01:26:26 +08:00
|
|
|
if (level <= FATAL) {
|
2017-08-12 00:03:37 +08:00
|
|
|
logTerm();
|
|
|
|
zmDbClose();
|
2021-02-26 01:26:26 +08:00
|
|
|
if (level <= PANIC) abort();
|
2018-03-30 00:24:00 +08:00
|
|
|
exit(-1);
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2019-09-17 23:10:59 +08:00
|
|
|
} // end logPrint
|
2018-11-15 06:02:52 +08:00
|
|
|
|
2018-06-09 03:21:43 +08:00
|
|
|
void logInit(const char *name, const Logger::Options &options) {
|
2021-02-26 01:26:26 +08:00
|
|
|
if (Logger::smInstance) {
|
2019-09-17 23:10:59 +08:00
|
|
|
delete Logger::smInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger::smInstance = new Logger();
|
|
|
|
Logger::smInstance->initialise(name, options);
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
2017-05-20 00:57:43 +08:00
|
|
|
void logTerm() {
|
2021-02-26 01:26:26 +08:00
|
|
|
if (Logger::smInstance) {
|
2016-04-04 22:11:48 +08:00
|
|
|
delete Logger::smInstance;
|
2020-08-26 07:45:48 +08:00
|
|
|
Logger::smInstance = nullptr;
|
2017-08-12 00:03:37 +08:00
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|