2011-06-21 17:19:10 +08:00
|
|
|
/*
|
|
|
|
* ZoneMinder Logger Interface, $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
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZM_LOGGER_H
|
|
|
|
#define ZM_LOGGER_H
|
|
|
|
|
2013-09-25 14:00:52 +08:00
|
|
|
#include "zm_config.h"
|
|
|
|
#include <stdint.h>
|
2012-07-17 17:23:13 +08:00
|
|
|
#include <unistd.h>
|
2011-06-21 17:19:10 +08:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2013-09-25 12:11:59 +08:00
|
|
|
#ifdef HAVE_SYS_SYSCALL_H
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#endif // HAVE_SYS_SYSCALL_H
|
2011-06-28 19:07:35 +08:00
|
|
|
#include <mysql/mysql.h>
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2018-11-24 01:45:41 +08:00
|
|
|
#include "zm_thread.h"
|
|
|
|
|
2017-08-11 03:44:20 +08:00
|
|
|
class Logger {
|
2011-06-21 17:19:10 +08:00
|
|
|
public:
|
2016-04-04 22:11:48 +08:00
|
|
|
enum {
|
|
|
|
NOOPT=-6,
|
2019-09-17 23:10:59 +08:00
|
|
|
NOLOG, // -5
|
|
|
|
PANIC, // -4
|
|
|
|
FATAL, // -3
|
|
|
|
ERROR, // -2
|
|
|
|
WARNING, // -1
|
|
|
|
INFO, // 0
|
2016-04-04 22:11:48 +08:00
|
|
|
DEBUG1,
|
|
|
|
DEBUG2,
|
|
|
|
DEBUG3,
|
|
|
|
DEBUG4,
|
|
|
|
DEBUG5,
|
|
|
|
DEBUG6,
|
|
|
|
DEBUG7,
|
|
|
|
DEBUG8,
|
|
|
|
DEBUG9
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef int Level;
|
|
|
|
|
|
|
|
typedef std::map<Level,std::string> StringMap;
|
|
|
|
typedef std::map<Level,int> IntMap;
|
|
|
|
|
2017-10-19 01:22:37 +08:00
|
|
|
class Options {
|
2016-04-04 22:11:48 +08:00
|
|
|
public:
|
2018-02-13 19:23:18 +08:00
|
|
|
int mTerminalLevel;
|
2016-04-04 22:11:48 +08:00
|
|
|
int mDatabaseLevel;
|
|
|
|
int mFileLevel;
|
|
|
|
int mSyslogLevel;
|
|
|
|
|
|
|
|
std::string mLogPath;
|
|
|
|
std::string mLogFile;
|
|
|
|
|
2019-09-17 23:10:59 +08:00
|
|
|
Options(
|
|
|
|
Level terminalLevel=NOOPT,
|
|
|
|
Level databaseLevel=NOOPT,
|
|
|
|
Level fileLevel=NOOPT,
|
|
|
|
Level syslogLevel=NOOPT,
|
|
|
|
const std::string &logPath=".",
|
|
|
|
const std::string &logFile=""
|
|
|
|
) :
|
|
|
|
mTerminalLevel(terminalLevel),
|
|
|
|
mDatabaseLevel(databaseLevel),
|
|
|
|
mFileLevel(fileLevel),
|
|
|
|
mSyslogLevel(syslogLevel),
|
|
|
|
mLogPath(logPath),
|
|
|
|
mLogFile(logFile)
|
2011-06-21 17:19:10 +08:00
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
|
|
|
};
|
2011-06-21 17:19:10 +08:00
|
|
|
|
|
|
|
private:
|
2016-04-04 22:11:48 +08:00
|
|
|
static bool smInitialised;
|
|
|
|
static Logger *smInstance;
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2018-11-24 01:45:41 +08:00
|
|
|
RecursiveMutex log_mutex;
|
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
static StringMap smCodes;
|
|
|
|
static IntMap smSyslogPriorities;
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
bool mInitialised;
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
std::string mId;
|
|
|
|
std::string mIdRoot;
|
|
|
|
std::string mIdArgs;
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2019-09-17 23:10:59 +08:00
|
|
|
Level mLevel; // Level that is currently in operation
|
2018-02-13 19:23:18 +08:00
|
|
|
Level mTerminalLevel; // Maximum level output via terminal
|
2019-09-17 23:10:59 +08:00
|
|
|
Level mDatabaseLevel; // Maximum level output via database
|
|
|
|
Level mFileLevel; // Maximum level output via file
|
|
|
|
Level mSyslogLevel; // Maximum level output via syslog
|
|
|
|
Level mEffectiveLevel; // Level optimised to take account of maxima
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
bool mDbConnected;
|
2019-09-17 23:10:59 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
std::string mLogPath;
|
|
|
|
std::string mLogFile;
|
|
|
|
FILE *mLogFileFP;
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2018-02-18 01:25:05 +08:00
|
|
|
bool mHasTerminal;
|
2016-04-04 22:11:48 +08:00
|
|
|
bool mFlush;
|
2011-06-21 17:19:10 +08:00
|
|
|
|
|
|
|
private:
|
2016-04-04 22:11:48 +08:00
|
|
|
Logger();
|
|
|
|
~Logger();
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2020-11-02 05:11:19 +08:00
|
|
|
int limit(const int level) const {
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( level > DEBUG9 )
|
2018-04-11 22:50:28 +08:00
|
|
|
return DEBUG9;
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( level < NOLOG )
|
2018-04-11 22:50:28 +08:00
|
|
|
return NOLOG;
|
|
|
|
return level;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
|
|
|
|
2018-03-30 00:24:12 +08:00
|
|
|
bool boolEnv(const std::string &name, bool defaultValue=false);
|
|
|
|
int intEnv(const std::string &name, bool defaultValue=0);
|
|
|
|
std::string strEnv(const std::string &name, const std::string &defaultValue="");
|
|
|
|
char *getTargettedEnv(const std::string &name);
|
2016-04-04 22:11:48 +08:00
|
|
|
|
|
|
|
void loadEnv();
|
2019-09-17 23:10:59 +08:00
|
|
|
static void usrHandler(int sig);
|
2011-06-21 17:19:10 +08:00
|
|
|
|
|
|
|
public:
|
2019-09-17 23:10:59 +08:00
|
|
|
friend void logInit(const char *name, const Options &options);
|
|
|
|
friend void logTerm();
|
|
|
|
|
|
|
|
static Logger *fetch() {
|
|
|
|
if ( !smInstance ) {
|
|
|
|
smInstance = new Logger();
|
|
|
|
Options options;
|
|
|
|
smInstance->initialise("undef", options);
|
|
|
|
}
|
|
|
|
return smInstance;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2019-09-17 23:10:59 +08:00
|
|
|
void initialise(const std::string &id, const Options &options);
|
|
|
|
void terminate();
|
|
|
|
|
2018-03-30 00:24:12 +08:00
|
|
|
const std::string &id(const std::string &id);
|
2019-09-17 23:10:59 +08:00
|
|
|
const std::string &id() const {
|
|
|
|
return mId;
|
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2018-02-13 19:23:18 +08:00
|
|
|
Level level() const {
|
2018-03-30 00:24:12 +08:00
|
|
|
return mLevel;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2018-04-11 22:50:28 +08:00
|
|
|
Level level(Level=NOOPT);
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2020-11-02 05:11:19 +08:00
|
|
|
bool debugOn() const {
|
2018-04-11 22:50:28 +08:00
|
|
|
return mEffectiveLevel >= DEBUG1;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2018-04-11 22:50:28 +08:00
|
|
|
Level terminalLevel(Level=NOOPT);
|
|
|
|
Level databaseLevel(Level=NOOPT);
|
|
|
|
Level fileLevel(Level=NOOPT);
|
|
|
|
Level syslogLevel(Level=NOOPT);
|
2011-06-21 17:19:10 +08:00
|
|
|
|
|
|
|
private:
|
2018-04-11 22:50:28 +08:00
|
|
|
void logFile(const std::string &logFile);
|
2016-04-04 22:11:48 +08:00
|
|
|
void openFile();
|
|
|
|
void closeFile();
|
|
|
|
void openSyslog();
|
|
|
|
void closeSyslog();
|
|
|
|
void closeDatabase();
|
2011-06-21 17:19:10 +08:00
|
|
|
|
|
|
|
public:
|
2018-04-11 22:50:28 +08:00
|
|
|
void logPrint(bool hex, const char * const filepath, const int line, const int level, const char *fstring, ...);
|
2011-06-21 17:19:10 +08:00
|
|
|
};
|
|
|
|
|
2018-04-11 22:50:28 +08:00
|
|
|
void logInit(const char *name, const Logger::Options &options=Logger::Options());
|
2011-06-21 17:19:10 +08:00
|
|
|
void logTerm();
|
2017-10-19 01:22:37 +08:00
|
|
|
inline const std::string &logId() {
|
2018-04-11 22:50:28 +08:00
|
|
|
return Logger::fetch()->id();
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
2017-10-19 01:22:37 +08:00
|
|
|
inline Logger::Level logLevel() {
|
2018-04-11 22:50:28 +08:00
|
|
|
return Logger::fetch()->level();
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
2017-10-19 01:22:37 +08:00
|
|
|
inline Logger::Level logDebugging() {
|
2018-04-11 22:50:28 +08:00
|
|
|
return Logger::fetch()->debugOn();
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define logPrintf(logLevel,params...) {\
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( logLevel <= Logger::fetch()->level() )\
|
|
|
|
Logger::fetch()->logPrint( false, __FILE__, __LINE__, logLevel, ##params );\
|
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
|
|
|
|
#define logHexdump(logLevel,data,len) {\
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( logLevel <= Logger::fetch()->level() )\
|
|
|
|
Logger::fetch()->logPrint( true, __FILE__, __LINE__, logLevel, "%p (%d)", data, len );\
|
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
|
|
|
|
/* Debug compiled out */
|
|
|
|
#ifndef DBG_OFF
|
|
|
|
#define Debug(level,params...) logPrintf(level,##params)
|
|
|
|
#define Hexdump(level,data,len) logHexdump(level,data,len)
|
|
|
|
#else
|
|
|
|
#define Debug(level,params...)
|
|
|
|
#define Hexdump(level,data,len)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Standard debug calls */
|
2016-04-04 22:11:48 +08:00
|
|
|
#define Info(params...) logPrintf(Logger::INFO,##params)
|
2011-06-21 17:19:10 +08:00
|
|
|
#define Warning(params...) logPrintf(Logger::WARNING,##params)
|
2016-04-04 22:11:48 +08:00
|
|
|
#define Error(params...) logPrintf(Logger::ERROR,##params)
|
|
|
|
#define Fatal(params...) logPrintf(Logger::FATAL,##params)
|
|
|
|
#define Panic(params...) logPrintf(Logger::PANIC,##params)
|
|
|
|
#define Mark() Info("Mark/%s/%d",__FILE__,__LINE__)
|
|
|
|
#define Log() Info("Log")
|
2011-06-21 17:19:10 +08:00
|
|
|
#ifdef __GNUC__
|
2016-04-04 22:11:48 +08:00
|
|
|
#define Enter(level) logPrintf(level,("Entering %s",__PRETTY_FUNCTION__))
|
|
|
|
#define Exit(level) logPrintf(level,("Exiting %s",__PRETTY_FUNCTION__))
|
2011-06-21 17:19:10 +08:00
|
|
|
#else
|
2016-04-04 22:11:48 +08:00
|
|
|
#define Enter(level)
|
|
|
|
#define Exit(level)
|
2011-06-21 17:19:10 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // ZM_LOGGER_H
|