📄 cplog.cxx
字号:
/* ==================================================================== * The Vovida Software License, Version 1.0 * * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The names "VOCAL", "Vovida Open Communication Application Library", * and "Vovida Open Communication Application Library (VOCAL)" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor * may "VOCAL" appear in their name, without prior written * permission of Vovida Networks, Inc. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc. For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * */static const char* const cpLog_cxx_Version = "$Id: cpLog.cxx,v 1.18 2002/09/06 08:58:15 bko Exp $";#include "global.h"#include <syslog.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/unistd.h>#include <stdarg.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <time.h>#include <errno.h>#include <cassert>#include <string>#include <map>#include "cpLog.h"#include "global.h"#include "Mutex.hxx"#include "VThread.hxx"#include "Lock.hxx"#include "support.hxx"using Vocal::Threads::Mutex;using Vocal::Threads::Lock;/* String sizes */#define DATEBUF_SIZE 256#define FILEBUF_SIZE 256#define LOG_FNAME_MAX_SIZE 256/* The trailing 0 is a nil pointer, for use by code that iterates overthe array and needs to know where to stop. */static const char* priName[] = { "EMERG", "ALERT", "CRIT", "ERR", "WARNING", "NOTICE", "INFO", "DEBUG", "DEBUG_STACK", "DEBUG_OPER", "DEBUG_HB", 0 };/* The trailing 0 is a nil pointer, for use by code that iterates overthe array and needs to know where to stop. */static const char* priNameShort[] = { "EMRG", "ALRT", "CRIT", "ERR ", "WARN", "NOTC", "INFO", "DBUG", "DSTK", "DOP ", "DHB ", 0 };static char cpLogFilename[LOG_FNAME_MAX_SIZE + 1] = "";static FILE* cpLogFd = stderr;static int numberOfBackupFilesToKeep = 6;/**The maximum size for a single log file. After the file reaches thissize, it will automatically be archived by the file-rotation algorithm.@see rotateFiles*/static int SIZE_PER_LOGFILE=6291456; /* in bytes, i.e. six megabytes *//* 1 if we're using syslog, 0 if we're not */int usingSyslog = 0;int openTheLogFile();void handleCriticalError (char* fmt, ...);inline void rotateFilesIfNecessary();void rotateFiles();static Mutex cpLogMutex;class CpLogPriority{ public: static int getPriority(); static void setPriority(int pri); static void setPriorityThread(vthread_t threadId, int pri); static void clearPriorityThread(vthread_t threadId); static const char* getLabel(); static void setLabel(const char* label); static void setLabelThread(vthread_t threadId, const char* label); static void clearLabelThread(vthread_t threadId); protected: CpLogPriority(); private: static Mutex logMutex; static CpLogPriority* getInstance(); static CpLogPriority* instance_; int logPriority; map < vthread_t, int > logPriorityMap; map < vthread_t, string > logLabelMap; string logLabel; /* This is a singleton class, so it makes no sense for it to have an assignment operator or a copy constructor. Keep the compiler from auto-generating by declaring them as private methods, and then refusing to define them. */ CpLogPriority (const CpLogPriority& x); CpLogPriority& operator= (const CpLogPriority& x);};CpLogPriority* CpLogPriority::instance_ = 0;Mutex CpLogPriority::logMutex;CpLogPriority::CpLogPriority() : logPriority(LOG_ERR), logLabel(""){}CpLogPriority* CpLogPriority::getInstance(){ if (!instance_) { instance_ = new CpLogPriority; } return instance_;}int CpLogPriority::getPriority(){ map < vthread_t, int > ::iterator i; Lock lock(logMutex); i = getInstance()->logPriorityMap.find(VThread::selfId()); if (i != getInstance()->logPriorityMap.end()) { // found it! return i->second; } else { // not found, use default return getInstance()->logPriority; }}voidCpLogPriority::setPriority(int pri){ assert (pri >= 0 && pri <= LAST_PRIORITY); Lock lock(logMutex); getInstance()->logPriority = pri;}voidCpLogPriority::setPriorityThread(vthread_t threadId, int pri){ assert (pri >= 0 && pri <= LAST_PRIORITY); Lock lock(logMutex); getInstance()->logPriorityMap[threadId] = pri;}voidCpLogPriority::clearPriorityThread(vthread_t threadId){ map < vthread_t, int > ::iterator i; Lock lock(logMutex); i = getInstance()->logPriorityMap.find(threadId); if (i != getInstance()->logPriorityMap.end()) { getInstance()->logPriorityMap.erase(i); }}const char* CpLogPriority::getLabel(){ map < vthread_t, string > ::iterator i; Lock lock(logMutex); i = getInstance()->logLabelMap.find(VThread::selfId()); if (i != getInstance()->logLabelMap.end()) { // found it! return i->second.c_str(); } else { // not found, use default return getInstance()->logLabel.c_str(); }}voidCpLogPriority::setLabel(const char* label){ Lock lock(logMutex); getInstance()->logLabel = label;}voidCpLogPriority::setLabelThread(vthread_t threadId, const char* label){ Lock lock(logMutex); getInstance()->logLabelMap[threadId] = label;}voidCpLogPriority::clearLabelThread(vthread_t threadId){ map < vthread_t, string > ::iterator i; Lock lock(logMutex); i = getInstance()->logLabelMap.find(threadId); if (i != getInstance()->logLabelMap.end()) { getInstance()->logLabelMap.erase(i); }}void cpLogOpenSyslog(){ #if !defined(WIN32) /* Use LOG_LOCAL7, which is reserved for non-system programs, to avoid gratuitously intermingling our logs with system logs. LOG_CONS means to print messages to the system console if there is some error in sending them to syslog. LOG_NDELAY means we will try to open syslog now, not after the first message is received. */ openlog (CpLogPriority::getLabel(), LOG_CONS | LOG_NDELAY, LOG_LOCAL7); usingSyslog = 1; #endif // !defined(WIN32)}intcpLogOpen (const char* filename){ assert (strlen (filename) <= FILEBUF_SIZE); strncpy (cpLogFilename, filename, FILEBUF_SIZE); usingSyslog = 0; return openTheLogFile();}intopenTheLogFile(){ if (!(cpLogFd = fopen (cpLogFilename, "a+"))) { handleCriticalError ("cpLog: Cannot open log file %s: %s", cpLogFilename, strerror(errno)); return 0; } return 1;}#ifndef __GNUC__#ifdef cpLog#undef cpLog#endifvoidcpLog(int pri, const char* fmt, ...){ if (pri < 0) return ; va_list ap; if (pri <= CpLogPriority::getPriority()) { Lock lock(cpLogMutex); va_start(ap, fmt); vCpLog(pri, "", 0, fmt, ap); va_end(ap); }} /* cpLog *//////////////////// ThienChange/////////////////void cpLog_old_version(int pri, const char* fmt, ...){ if (pri < 0) return ; va_list ap; if (pri <= CpLogPriority::getPriority()) { Lock lock(cpLogMutex); va_start(ap, fmt); vCpLog(pri, "", 0, fmt, ap); va_end(ap); }} /* cpLog */WrapLog::WrapLog(const char *fname, int lineno) : m_fname(fname), m_lineno(lineno) {}void WrapLog::operator() (int pri, const char *fmt, ...){ if (pri < 0) return ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -