00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00099 void header(CSThread *self, const char *func, const char *file, int line, int level);
00100
00101
00102 FILE *iStream;
00103
00104 bool iHeaderPending;
00105
00106 int iLogLevel;
00107
00108 pthread_t iLockThread;
00109 int iLockCount;
00110 pthread_mutex_t iMutex;
00111 };
00112
00113 extern CSLog CSL;
00114
00115 #endif