Drizzled Public API Documentation

CSLog.h
00001 /* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
00002  *
00003  * PrimeBase Media Stream for MySQL
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00018  *
00019  * Original author: Paul McCullagh (H&G2JCtL)
00020  * Continued development: Barry Leslie
00021  *
00022  * 2007-05-20
00023  *
00024  * CORE SYSTEM:
00025  * General logging class
00026  *
00027  */
00028 
00029 #pragma once
00030 #ifndef __CSLOG_H__
00031 #define __CSLOG_H__
00032 
00033 #include <stdio.h>
00034 #include <stdarg.h>
00035 
00036 
00037 #include "CSDefs.h"
00038 #include "CSString.h"
00039 
00040 class CSLog {
00041 public:
00042   static const int Protocol = 0;
00043   static const int Error = 1;
00044   static const int Warning = 2;
00045   static const int Trace = 3;
00046 
00047   CSLog(FILE *s, int level):
00048     iStream(s),
00049     iHeaderPending(true),
00050     iLogLevel(level),
00051     iLockCount(0)
00052      {
00053     pthread_mutex_init(&iMutex, NULL);
00054     memset(&iLockThread, 0, sizeof(iLockThread));
00055   }
00056 
00057   virtual ~CSLog() {
00058     memset(&iLockThread, 0, sizeof(iLockThread));
00059     iLockCount = 0;
00060     pthread_mutex_destroy(&iMutex);
00061   }
00062 
00063   void lock() {
00064     pthread_t thd = pthread_self();
00065 
00066     if (iLockCount > 0 && pthread_equal(iLockThread, thd))
00067       iLockCount++;
00068     else {
00069       pthread_mutex_lock(&iMutex);
00070       iLockThread = thd;
00071       iLockCount = 1;
00072     }
00073   }
00074 
00075   void unlock() {
00076     if (iLockCount > 0) {
00077       iLockCount--;
00078       if (iLockCount == 0)
00079         pthread_mutex_unlock(&iMutex);
00080     }
00081   }
00082 
00083   void getNow(char *buffer, size_t len);
00084   void log(CSThread *self, const char *func, const char *file, int line, int level, const char* buffer);
00085   void log(CSThread *self, int level, const char*);
00086   void log(CSThread *self, int level, CSString&);
00087   void log(CSThread *self, int level, CSString*);
00088   void log(CSThread *self, int level, int);
00089   void eol(CSThread *self, int level);
00090 
00091   void logLine(CSThread *self, int level, const char *buffer);
00092   void log_va(CSThread *self, int level, const char *func, const char *file, int line, const char *fmt, va_list ap);
00093   void logf(CSThread *self, int level, const char *fmt, ...);
00094   void logf(CSThread *self, int level, const char *func, const char *file, int line, const char *fmt, ...);
00095   
00096   void flush() {fflush(iStream);}
00097 private:
00098   /* Write out a logging header: */
00099   void header(CSThread *self, const char *func, const char *file, int line, int level);
00100 
00101   /* The output stream: */
00102   FILE *iStream;
00103 
00104   bool iHeaderPending;    /* True if we must write a header before the next text. */
00105 
00106   int iLogLevel;        /* The current log level. */
00107 
00108   pthread_t iLockThread;
00109   int iLockCount;
00110   pthread_mutex_t iMutex;
00111 };
00112 
00113 extern CSLog CSL;
00114 
00115 #endif