Changeset 12 in libcf
- Timestamp:
- 01/31/13 14:40:55 (11 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/cf_file.h
r6 r12 25 25 26 26 CF_EXPORT int 27 CF_F ILE_Close (const int fd);27 CF_File_Close (const int fd); 28 28 29 29 CF_EXPORT int -
trunk/include/cf_log.h
r11 r12 8 8 9 9 #define CF_ERROR_LOG_INITIALIZE CF_ERROR_LOG - 1 10 #define CF_ERROR_LOG_INVALID_CTX CF_ERROR_LOG - 2 11 #define CF_ERROR_LOG_SET_MULTITHREAD CF_ERROR_LOG - 3 12 #define CF_ERROR_LOG_UNSET_MULTITHREAD CF_ERROR_LOG - 4 13 #define CF_ERROR_LOG_LOCK_CTX CF_ERROR_LOG - 5 14 #define CF_ERROR_LOG_UNLOCK_CTX CF_ERROR_LOG - 6 15 #define CF_ERROR_LOG_FLUSH CF_ERROR_LOG - 7 10 16 11 typedef enum { 12 CF_LOG_WITHOUT_MT = 0, 13 CF_LOG_WITH_MT 14 } E_CF_LOG_MT, CF_LOG_MT; 17 #define CF_LOG_BUFFER_DEFAULT -1 18 #define CF_LOG_BUFFER_NO 0 15 19 16 20 typedef void * CF_Log_Ctx; … … 21 25 22 26 CF_EXPORT int 23 CF_Log_Initialize ( void);27 CF_Log_Initialize (const int logPool); 24 28 25 29 CF_EXPORT int 26 30 CF_Log_Finalize (void); 27 31 32 CF_EXPORT CF_Log_Ctx 33 CF_Log_CreateCtx (const char * path, 34 const int memsize); 35 28 36 CF_EXPORT int 29 CF_Log_CreateCtx (CF_Log_Ctx ctx, 30 const char * path, 31 const int bufferLength, 32 CF_LOG_MT isMT); 37 CF_Log_SetMultiThread (CF_Log_Ctx ctx); 38 39 CF_EXPORT int 40 CF_Log_UnsetMultiThread (CF_Log_Ctx ctx); 41 42 CF_EXPORT int 43 CF_Log_DestroyCtx (CF_Log_Ctx ctx); 44 45 CF_EXPORT int 46 CF_Log_Write (CF_Log_Ctx ctx, 47 const char * prefix, 48 const char * fmt, ...); 49 50 CF_EXPORT int 51 CF_Log_Flush (CF_Log_Ctx ctx); 33 52 34 53 #ifdef __cplusplus -
trunk/src/cf_debug.c
r10 r12 50 50 51 51 void 52 CF_Debug_ VA_Message(CF_DEBUG_CTX * ctx,53 const char* fmt,54 va_list valist)52 CF_Debug_Local_Print (CF_DEBUG_CTX * ctx, 53 const char * fmt, 54 va_list valist) 55 55 { 56 56 fprintf (GET_CTX_OSTREAM (ctx), "[DEBUG][%s %s(%d)] ", … … 142 142 143 143 va_start (valist, fmt); 144 CF_Debug_ VA_Message((CF_DEBUG_CTX *) ctx, fmt, valist);144 CF_Debug_Local_Print ((CF_DEBUG_CTX *) ctx, fmt, valist); 145 145 va_end (valist); 146 146 … … 161 161 162 162 va_start (valist, fmt); 163 CF_Debug_ VA_Message((CF_DEBUG_CTX *) ctx, fmt, valist);163 CF_Debug_Local_Print ((CF_DEBUG_CTX *) ctx, fmt, valist); 164 164 165 165 for (i = 0 ; i < len ; i += 16) -
trunk/src/cf_local.h
r4 r12 7 7 #define TRY do 8 8 #define TRY_BREAK break 9 #define CATCH_IF(__expr) while (0); if (__expr) 9 #define NO_CATCH while (0) 10 #define CATCH_IF(__expr) NO_CATCH; if (__expr) 10 11 #define CATCH_ELSE_IF(__expr) else if (__expr) 11 12 #define CATCH_ELSE else -
trunk/src/cf_log.c
r11 r12 3 3 */ 4 4 #include "cf_log.h" 5 #include "cf_file.h" 5 6 #include "cf_thread.h" 6 7 #include "cf_local.h" 8 9 #include <stdio.h> 10 #include <stdlib.h> 7 11 #include <string.h> 12 #include <stdarg.h> 13 #include <time.h> 8 14 9 15 #ifdef _WIN32 10 16 #else // #ifdef _WIN32 11 17 # include <sys/stat.h> 18 # include <sys/time.h> 12 19 #endif // #ifdef _WIN32 20 21 #define CHECK_INVALID_CTX(__ctx) \ 22 if (__ctx == NULL) \ 23 return CF_ERROR_LOG_INVALID_CTX 24 25 #define LOCK_LOG_CTX(__ctx) \ 26 if (CF_Mutex_Lock (&__ctx->mutex) < 0) \ 27 return CF_ERROR_LOG_LOCK_CTX 28 29 #define UNLOCK_LOG_CTX(__ctx) \ 30 if (CF_Mutex_Unlock (&__ctx->mutex) < 0) \ 31 return CF_ERROR_LOG_UNLOCK_CTX 32 33 #define CF_LOG_BUFFER_DEFAULT_SIZE 128 * 1024 34 35 typedef struct __cf_util_datetime__ 36 { 37 int year; 38 int month; 39 int day; 40 int week; /* SUN:0 ~ SAT:6 */ 41 42 int hour; 43 int min; 44 int sec; 45 int usec; 46 } S_CF_Log_DateTime, CF_Log_DateTime; 13 47 14 48 typedef struct __cf_log_ctx__ { … … 16 50 int fd; 17 51 char * buffer; 18 int entireLength;19 int bufferLength;52 size_t size; /* entire size of buffer */ 53 size_t length; /* data length in current */ 20 54 CF_Mutex mutex; 21 55 } S_CF_LOG_CTX, CF_LOG_CTX; … … 23 57 typedef struct __cf_log_environment__ { 24 58 __blksize_t fsBlockSize; 25 } S_CF_Log_Environment, CF_Log_Environment; 26 27 CF_Log_Environment gLogEnvironment; 28 29 int 30 CF_Log_Initialize (void) 59 CF_Log_Ctx * ctxPool; 60 int ctxSize; 61 } S_CF_LOG_ENVIRONMENT, CF_LOG_ENVIRONMENT; 62 63 CF_LOG_ENVIRONMENT gLogEnvironment; 64 65 #if defined(_WIN32) || defined(_WIN64) 66 /* #if defined(_WIN32) || defined(_WIN64) {{{ */ 67 struct timezone 68 { 69 int tz_minuteswest; /* minutes W of Greenwich */ 70 int tz_dsttime; /* type of dst correction */ 71 }; 72 73 // Definition of a gettimeofday function 74 int gettimeofday(struct timeval *tv, struct timezone *tz) 75 { 76 // Define a structure to receive the current Windows filetime 77 FILETIME ft; 78 79 // Initialize the present time to 0 and the timezone to UTC 80 unsigned __int64 ui64 =0; 81 static int tzflag = 0; 82 83 if (NULL != tv) 84 { 85 GetSystemTimeAsFileTime(&ft); 86 87 ui64 = (((unsigned __int64) ft.dwHighDateTime << 32) 88 + (unsigned __int64) ft.dwLowDateTime); 89 90 if (ui64) 91 { 92 ui64 /= 10; 93 ui64 -= ((369 * 365 + 89) * (unsigned __int64) 86400) * 1000000; 94 } 95 96 // Finally change microseconds to seconds and place in the seconds value. 97 // The modulus picks up the microseconds. 98 tv->tv_sec = (long)(ui64 / 1000000UL); 99 tv->tv_usec = (long)(ui64 % 1000000UL); 100 } 101 102 if (NULL != tz) 103 { 104 if (!tzflag) 105 { 106 _tzset(); 107 tzflag++; 108 } 109 110 // Adjust for the timezone west of Greenwich 111 tz->tz_minuteswest = _timezone / 60; 112 tz->tz_dsttime = _daylight; 113 } 114 115 return 0; 116 } 117 #endif // #if defined(_WIN32) || defined(_WIN64) 118 /* }}} #if defined(_WIN32) || defined(_WIN64) */ 119 120 int 121 CF_Log_Local_GetTime (CF_Log_DateTime * dt) 122 { 123 struct timeval timeVal; 124 struct tm * timeSt; 125 126 gettimeofday (&timeVal, NULL); 127 timeSt = localtime (&timeVal.tv_sec); 128 129 dt->year = timeSt->tm_year + 1900; 130 dt->month = timeSt->tm_mon + 1; 131 dt->day = timeSt->tm_mday; 132 dt->week = timeSt->tm_wday; 133 134 dt->hour = timeSt->tm_hour; 135 dt->min = timeSt->tm_min; 136 dt->sec = timeSt->tm_sec; 137 138 dt->usec = (int) timeVal.tv_usec; 139 140 return CF_OK; 141 } 142 143 int 144 CF_Log_Local_GetTimeString (char * buffer) 145 { 146 CF_Log_DateTime dt; 147 CF_Log_Local_GetTime (&dt); 148 149 sprintf (buffer, "%02d-%02d-%02d %02d:%02d:%02d.%03d", 150 dt.year, dt.month, dt.day, 151 dt.hour, dt.min, dt.sec, dt.usec); 152 153 return CF_OK; 154 } 155 156 int 157 CF_Log_Local_Push (CF_LOG_CTX * ctx, 158 const char * buffer) 159 { 160 strncat (ctx->buffer + ctx->length, 161 buffer, 162 ctx->size - ctx->length); 163 ctx->length = strlen (ctx->buffer); 164 165 return CF_OK; 166 } 167 168 int 169 CF_Log_Local_Flush (CF_LOG_CTX * ctx) 170 { 171 if (CF_File_Write (ctx->fd, ctx->buffer, ctx->length) < 0) 172 return CF_ERROR_LOG_FLUSH; 173 174 memset (ctx->buffer, 0x00, ctx->size); 175 ctx->length = 0; 176 177 return CF_OK; 178 } 179 180 int 181 CF_Log_Initialize (const int logPool) 31 182 { 32 183 struct stat fsstat; 33 184 34 memset (&gLogEnvironment, 0x00, sizeof (CF_L og_Environment));35 36 if (stat (". /", &fsstat) < 0)185 memset (&gLogEnvironment, 0x00, sizeof (CF_LOG_ENVIRONMENT)); 186 187 if (stat (".", &fsstat) < 0) 37 188 return CF_ERROR_LOG_INITIALIZE; 38 189 39 190 gLogEnvironment.fsBlockSize = fsstat.st_blksize; 191 if (logPool > 0) 192 { 193 gLogEnvironment.ctxPool = 194 (CF_Log_Ctx *) calloc ((size_t) logPool, sizeof (CF_Log_Ctx)); 195 gLogEnvironment.ctxSize = logPool; 196 } 40 197 41 198 return CF_OK; … … 45 202 CF_Log_Finalize (void) 46 203 { 47 memset (&gLogEnvironment, 0x00, sizeof (CF_Log_Environment)); 48 49 return CF_OK; 50 } 204 memset (&gLogEnvironment, 0x00, sizeof (CF_LOG_ENVIRONMENT)); 205 206 return CF_OK; 207 } 208 209 CF_Log_Ctx 210 CF_Log_CreateCtx (const char * path, 211 const int memsize) 212 { 213 int result = 0; 214 int size = (memsize == CF_LOG_BUFFER_DEFAULT) 215 ? CF_LOG_BUFFER_DEFAULT_SIZE 216 : memsize; 217 CF_LOG_CTX * context = NULL; 218 219 if (path == NULL) 220 return NULL; 221 222 TRY 223 { 224 context = (CF_LOG_CTX *) calloc (sizeof (CF_LOG_CTX), 1); 225 if (context == NULL) 226 { 227 result = -1; 228 TRY_BREAK; 229 } 230 231 context->fd = CF_File_Create (path); 232 if (context->fd < 0) 233 { 234 result = -2; 235 TRY_BREAK; 236 } 237 sprintf (context->path, "%s", path); 238 239 if (size > 0) 240 { 241 context->buffer = (char *) calloc ((size_t) size + 1, 1); 242 if (context->buffer == NULL) 243 { 244 result = -3; 245 TRY_BREAK; 246 } 247 context->size = (size_t) size; 248 } 249 } 250 CATCH_IF (result < 0) 251 { 252 CF_Log_DestroyCtx ((CF_Log_Ctx) context); 253 return NULL; 254 } 255 256 return (CF_Log_Ctx) context; 257 } 258 259 int 260 CF_Log_SetMultiThread (CF_Log_Ctx ctx) 261 { 262 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx; 263 264 CHECK_INVALID_CTX (ctx); 265 266 if (CF_Mutex_Create (&context->mutex) < 0) 267 return CF_ERROR_LOG_SET_MULTITHREAD; 268 269 return CF_OK; 270 } 271 272 int 273 CF_Log_UnsetMultiThread (CF_Log_Ctx ctx) 274 { 275 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx; 276 277 CHECK_INVALID_CTX (ctx); 278 279 if (CF_Mutex_Destory (&context->mutex) < 0) 280 return CF_ERROR_LOG_UNSET_MULTITHREAD; 281 282 return CF_OK; 283 } 284 285 int 286 CF_Log_DestroyCtx (CF_Log_Ctx ctx) 287 { 288 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx; 289 290 CHECK_INVALID_CTX (ctx); 291 292 memset (context->path, 0x00, sizeof (context->path)); 293 294 if (context->buffer != NULL) 295 { 296 /* FIXME : write residual data in buffer */ 297 298 free (context->buffer); 299 context->buffer = NULL; 300 context->size = 0; 301 } 302 303 CF_File_Close (context->fd); 304 305 if (context->mutex != NULL) 306 { 307 if (CF_Mutex_Destory (&context->mutex) < 0) 308 /* do something ? */; 309 310 context->mutex = NULL; 311 } 312 313 return CF_OK; 314 } 315 316 int 317 CF_Log_Write (CF_Log_Ctx ctx, 318 const char * prefix, 319 const char * fmt, ...) 320 { 321 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx; 322 va_list valist; 323 char buffer[4096] = {0x00,}; 324 size_t length = 0; 325 326 CHECK_INVALID_CTX (ctx); 327 328 LOCK_LOG_CTX (context); 329 va_start (valist, fmt); 330 331 strncat (buffer, "[", 1); 332 CF_Log_Local_GetTimeString (buffer + strlen (buffer)); 333 sprintf (buffer + strlen (buffer), "][%s] ", prefix); 334 vsprintf (buffer + strlen (buffer), fmt, valist); 335 336 if (context->size == CF_LOG_BUFFER_NO) 337 { 338 context->buffer = buffer; 339 context->length = strlen (buffer); 340 341 CF_Log_Local_Flush (context); 342 343 context->buffer = NULL; 344 /* context->length = 0; // already did this in flush */ 345 } 346 else /* (context->size > 0) */ 347 { 348 length = context->size - context->length; 349 CF_Log_Local_Push (context, buffer); 350 351 if (context->length == context->size) 352 { 353 CF_Log_Local_Flush (context); 354 CF_Log_Local_Push (context, buffer + length); 355 } 356 } 357 358 va_end (valist); 359 UNLOCK_LOG_CTX (context); 360 361 return CF_OK; 362 } 363 364 int 365 CF_Log_Flush (CF_Log_Ctx ctx) 366 { 367 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx; 368 369 CHECK_INVALID_CTX (ctx); 370 371 LOCK_LOG_CTX (context); 372 CF_Log_Local_Flush (context); 373 UNLOCK_LOG_CTX (context); 374 375 return CF_OK; 376 }
Note:
See TracChangeset
for help on using the changeset viewer.