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
|
|
|
|
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,
|
|
|
|
NOLOG,
|
|
|
|
PANIC,
|
|
|
|
FATAL,
|
|
|
|
ERROR,
|
|
|
|
WARNING,
|
|
|
|
INFO,
|
|
|
|
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;
|
|
|
|
|
|
|
|
public:
|
2018-02-18 01:25:05 +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 ),
|
2016-04-04 22:11:48 +08:00
|
|
|
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
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
static StringMap smCodes;
|
|
|
|
static IntMap smSyslogPriorities;
|
2011-06-21 17:19:10 +08:00
|
|
|
|
|
|
|
private:
|
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
|
|
|
|
2016-04-04 22:11:48 +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
|
2016-04-04 22:11:48 +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;
|
|
|
|
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
|
|
|
static void usrHandler( int sig );
|
2011-06-21 17:19:10 +08:00
|
|
|
|
|
|
|
public:
|
2016-04-04 22:11:48 +08:00
|
|
|
friend void logInit( const char *name, const Options &options );
|
|
|
|
friend void logTerm();
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2018-02-13 19:23:18 +08:00
|
|
|
static Logger *fetch() {
|
|
|
|
if ( !smInstance ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
smInstance = new Logger();
|
|
|
|
Options options;
|
|
|
|
smInstance->initialise( "undef", options );
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
2016-04-04 22:11:48 +08:00
|
|
|
return( smInstance );
|
|
|
|
}
|
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
|
|
|
|
|
|
|
public:
|
2016-04-04 22:11:48 +08:00
|
|
|
void initialise( const std::string &id, const Options &options );
|
|
|
|
void terminate();
|
2011-06-21 17:19:10 +08:00
|
|
|
|
|
|
|
private:
|
2018-02-13 19:23:18 +08:00
|
|
|
int limit( int level ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( level > DEBUG9 )
|
|
|
|
return( DEBUG9 );
|
|
|
|
if ( level < NOLOG )
|
|
|
|
return( NOLOG );
|
|
|
|
return( level );
|
|
|
|
}
|
|
|
|
|
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();
|
2011-06-21 17:19:10 +08:00
|
|
|
|
|
|
|
public:
|
2018-02-13 19:23:18 +08:00
|
|
|
const std::string &id() const {
|
2018-03-30 00:24:12 +08:00
|
|
|
return mId;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2018-03-30 00:24:12 +08:00
|
|
|
const std::string &id(const std::string &id);
|
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
|
|
|
}
|
|
|
|
Level level( Level=NOOPT );
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2017-10-19 01:22:37 +08:00
|
|
|
bool debugOn() {
|
2016-04-04 22:11:48 +08:00
|
|
|
return( mEffectiveLevel >= DEBUG1 );
|
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
|
2018-02-18 01:25:05 +08:00
|
|
|
Level terminalLevel( Level=NOOPT );
|
2016-04-04 22:11:48 +08:00
|
|
|
Level databaseLevel( Level=NOOPT );
|
|
|
|
Level fileLevel( Level=NOOPT );
|
|
|
|
Level syslogLevel( Level=NOOPT );
|
2011-06-21 17:19:10 +08:00
|
|
|
|
|
|
|
private:
|
2016-04-04 22:11:48 +08:00
|
|
|
void logFile( const std::string &logFile );
|
|
|
|
void openFile();
|
|
|
|
void closeFile();
|
|
|
|
void openSyslog();
|
|
|
|
void closeSyslog();
|
|
|
|
void closeDatabase();
|
2011-06-21 17:19:10 +08:00
|
|
|
|
|
|
|
public:
|
2016-04-04 22:11:48 +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
|
|
|
};
|
|
|
|
|
|
|
|
void logInit( const char *name, const Logger::Options &options=Logger::Options() );
|
|
|
|
void logTerm();
|
2017-10-19 01:22:37 +08:00
|
|
|
inline const std::string &logId() {
|
2016-04-04 22:11:48 +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() {
|
2016-04-04 22:11:48 +08:00
|
|
|
return( Logger::fetch()->level() );
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
2017-10-19 01:22:37 +08:00
|
|
|
inline void logCapLevel( Logger::Level level ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
Logger::fetch()->level( level );
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
2017-10-19 01:22:37 +08:00
|
|
|
inline Logger::Level logDebugging() {
|
2016-04-04 22:11:48 +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
|