协能can协议
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356
  1. /*
  2. * iLOG3 - log function library written in c
  3. * author : calvin
  4. * email : calvinwilliams.c@gmail.com
  5. * LastVersion : v1.0.11
  6. *
  7. * Licensed under the LGPL v2.1, see the file LICENSE in base directory.
  8. */
  9. #if ( defined _WIN32 )
  10. #ifndef _WINDLL_FUNC
  11. #define _WINDLL_FUNC _declspec(dllexport)
  12. #endif
  13. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  14. #ifndef _WINDLL_FUNC
  15. #define _WINDLL_FUNC
  16. #endif
  17. #endif
  18. #include "LOG.h"
  19. /* ��־�ȼ��������ձ� */ /* log level describe */
  20. static char sg_aszLogLevelDesc[][5+1] = { "DEBUG" , "INFO" , "WARN" , "ERROR" , "FATAL" , "NOLOG" } ;
  21. /* �汾��ʶ */ /* version */
  22. _WINDLL_FUNC int _LOG_VERSION_1_0_14 = 0 ;
  23. /* �̱߳��ش洢ȫ�ֶ��� */ /* TLS */
  24. #if ( defined _WIN32 )
  25. __declspec( thread ) LOG *tls_g = NULL ;
  26. #elif ( defined __linux__ ) || ( defined _AIX )
  27. __thread LOG *tls_g = NULL ;
  28. #endif
  29. /* �ٽ��� */ /* critical region */
  30. #if ( defined __linux__ ) || ( defined __unix ) || ( defined _AIX )
  31. pthread_mutex_t g_pthread_mutex = PTHREAD_MUTEX_INITIALIZER ;
  32. #endif
  33. static int CreateMutexSection( LOG *g )
  34. {
  35. #if ( defined _WIN32 )
  36. char lock_pathfilename[ MAXLEN_FILENAME + 1 ] ;
  37. strcpy( lock_pathfilename , "Global\\iLOG3_ROTATELOCK" );
  38. g->rotate_lock = CreateMutex( NULL , FALSE , lock_pathfilename ) ;
  39. if( g->rotate_lock == NULL )
  40. return LOG_RETURN_ERROR_INTERNAL;
  41. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  42. char lock_pathfilename[ MAXLEN_FILENAME + 1 ] ;
  43. mode_t m ;
  44. SNPRINTF( lock_pathfilename , sizeof(lock_pathfilename) , "/tmp/iLOG3.lock" );
  45. m=umask(0);
  46. g->rotate_lock = open( lock_pathfilename , O_CREAT|O_APPEND|O_WRONLY , S_IRWXU|S_IRWXG|S_IRWXO ) ;
  47. umask(m);
  48. if( g->rotate_lock == FD_NULL )
  49. return LOG_RETURN_ERROR_INTERNAL;
  50. #endif
  51. return 0;
  52. }
  53. static int DestroyMutexSection( LOG *g )
  54. {
  55. #if ( defined _WIN32 )
  56. if( g->rotate_lock )
  57. {
  58. CloseHandle( g->rotate_lock );
  59. }
  60. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  61. if( g->rotate_lock != -1 )
  62. {
  63. close( g->rotate_lock );
  64. }
  65. #endif
  66. return 0;
  67. }
  68. static int EnterMutexSection( LOG *g )
  69. {
  70. #if ( defined _WIN32 )
  71. DWORD dw ;
  72. dw = WaitForSingleObject( g->rotate_lock , INFINITE ) ;
  73. if( dw != WAIT_OBJECT_0 )
  74. return LOG_RETURN_ERROR_INTERNAL;
  75. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  76. int nret ;
  77. memset( & (g->lock) , 0x00 , sizeof(g->lock) );
  78. g->lock.l_type = F_WRLCK ;
  79. g->lock.l_whence = SEEK_SET ;
  80. g->lock.l_start = 0 ;
  81. g->lock.l_len = 0 ;
  82. nret = fcntl( g->rotate_lock , F_SETLKW , & (g->lock) ) ;
  83. if( nret == -1 )
  84. return LOG_RETURN_ERROR_INTERNAL;
  85. pthread_mutex_lock( & g_pthread_mutex );
  86. #endif
  87. return 0;
  88. }
  89. static int LeaveMutexSection( LOG *g )
  90. {
  91. #if ( defined _WIN32 )
  92. BOOL bret ;
  93. bret = ReleaseMutex( g->rotate_lock ) ;
  94. if( bret != TRUE )
  95. return LOG_RETURN_ERROR_INTERNAL;
  96. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  97. int nret ;
  98. pthread_mutex_unlock( & g_pthread_mutex );
  99. memset( & (g->lock) , 0x00 , sizeof(g->lock) );
  100. g->lock.l_type = F_UNLCK ;
  101. g->lock.l_whence = SEEK_SET ;
  102. g->lock.l_start = 0 ;
  103. g->lock.l_len = 0 ;
  104. nret = fcntl( g->rotate_lock , F_SETLK , & (g->lock) ) ;
  105. if( nret == -1 )
  106. return LOG_RETURN_ERROR_INTERNAL;
  107. #endif
  108. return 0;
  109. }
  110. /* ������������С */ /* adjust buffer size */
  111. static int SetBufferSize( LOG *g , LOGBUF *logbuf , long buf_size , long max_buf_size )
  112. {
  113. int nret = 0 ;
  114. if( g == NULL )
  115. return LOG_RETURN_ERROR_PARAMETER;
  116. if( logbuf == NULL )
  117. return LOG_RETURN_ERROR_PARAMETER;
  118. if( max_buf_size != -1 )
  119. {
  120. logbuf->max_buf_size = max_buf_size ;
  121. }
  122. if( buf_size != -1 )
  123. {
  124. if( logbuf->bufbase == NULL )
  125. {
  126. nret = EnterMutexSection( g ) ;
  127. if( nret )
  128. return nret;
  129. logbuf->bufbase = (char*)malloc( buf_size ) ;
  130. LeaveMutexSection( g );
  131. if( logbuf->bufbase == NULL )
  132. return LOG_RETURN_ERROR_ALLOC;
  133. logbuf->buf_size = buf_size ;
  134. memset( logbuf->bufbase , 0x00 , logbuf->buf_size );
  135. }
  136. else
  137. {
  138. char *tmp = NULL ;
  139. nret = EnterMutexSection( g ) ;
  140. if( nret )
  141. return nret;
  142. tmp = (char*)realloc( logbuf->bufbase , buf_size ) ;
  143. LeaveMutexSection( g );
  144. if( tmp == NULL )
  145. return LOG_RETURN_ERROR_ALLOC;
  146. logbuf->buf_remain_len = logbuf->buf_remain_len + ( buf_size - logbuf->buf_size ) ;
  147. logbuf->bufptr = logbuf->bufptr - logbuf->bufbase + tmp ;
  148. logbuf->bufbase = tmp ;
  149. logbuf->buf_size = buf_size ;
  150. }
  151. }
  152. return 0;
  153. }
  154. /* ������־��� */ /* destruction of log handle */
  155. void DestroyLogHandle( LOG *g )
  156. {
  157. if( g )
  158. {
  159. if( g->logbuf.bufbase )
  160. {
  161. free( g->logbuf.bufbase );
  162. }
  163. if( g->hexlogbuf.bufbase )
  164. {
  165. free( g->hexlogbuf.bufbase );
  166. }
  167. if( g->open_flag == 1 )
  168. {
  169. if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_ONCE ) || TEST_ATTRIBUTE( g->log_options , LOG_OPTION_CHANGE_TEST ) )
  170. {
  171. if( g->pfuncCloseLogFinally )
  172. {
  173. g->pfuncCloseLogFinally( g , & (g->open_handle) );
  174. g->open_flag = 0 ;
  175. }
  176. }
  177. }
  178. DestroyMutexSection( g );
  179. memset( g , 0x00 , sizeof(LOG) );
  180. free(g);
  181. }
  182. return;
  183. }
  184. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  185. void DestroyLogHandleG()
  186. {
  187. DestroyLogHandle( tls_g );
  188. }
  189. #endif
  190. /* ������־��� */ /* create log handle */
  191. LOG *CreateLogHandle()
  192. {
  193. LOG _g , *g = NULL ;
  194. int nret ;
  195. #if ( defined _WIN32 )
  196. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  197. #else
  198. return LOG_RETURN_ERROR_NOTSUPPORT;
  199. #endif
  200. memset( & _g , 0x00 , sizeof(LOG) );
  201. nret = CreateMutexSection( & _g ) ;
  202. if( nret )
  203. return NULL;
  204. nret = EnterMutexSection( & _g ) ;
  205. if( nret )
  206. return NULL;
  207. g = (LOG *)malloc( sizeof(LOG) ) ;
  208. LeaveMutexSection( & _g );
  209. if( g == NULL )
  210. return NULL;
  211. memcpy( g , & _g , sizeof(LOG) );
  212. SetLogLevel( g , LOG_LEVEL_DEFAULT );
  213. SetLogStyles( g , LOG_STYLES_DEFAULT , LOG_NO_STYLEFUNC );
  214. SetLogOptions( g , LOG_OPTION_CHANGE_TEST );
  215. g->rotate_mode = LOG_ROTATEMODE_NONE ;
  216. g->rotate_file_no = 1 ;
  217. g->rotate_file_count = LOG_ROTATE_SIZE_FILE_COUNT_DEFAULT ;
  218. g->pressure_factor = LOG_ROTATE_SIZE_PRESSURE_FACTOR_DEFAULT ;
  219. g->fsync_period = LOG_FSYNC_PERIOD ;
  220. g->fsync_elapse = g->fsync_period ;
  221. g->logbuf.buf_size = 0 ;
  222. g->logbuf.bufbase = NULL ;
  223. g->logbuf.bufptr = NULL ;
  224. g->logbuf.buf_remain_len = 0 ;
  225. nret = SetLogBufferSize( g , LOG_BUFSIZE_DEFAULT , LOG_BUFSIZE_MAX ) ;
  226. if( nret )
  227. {
  228. DestroyLogHandle( g );
  229. return NULL;
  230. }
  231. g->hexlogbuf.buf_size = 0 ;
  232. g->hexlogbuf.bufbase = NULL ;
  233. g->hexlogbuf.bufptr = NULL ;
  234. g->hexlogbuf.buf_remain_len = 0 ;
  235. nret = SetHexLogBufferSize( g , LOG_HEXLOG_BUFSIZE_DEFAULT , LOG_HEXLOG_BUFSIZE_MAX ) ;
  236. if( nret )
  237. {
  238. DestroyLogHandle( g );
  239. return NULL;
  240. }
  241. return g;
  242. }
  243. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  244. LOG *CreateLogHandleG()
  245. {
  246. tls_g = CreateLogHandle() ;
  247. return tls_g;
  248. }
  249. #endif
  250. /* �򿪡�������ر���־���� */ /* open , write , close log functions */
  251. #if ( defined _WIN32 )
  252. #define LOG_WINOPENFILE_TRYCOUNT 1000 /* windows�϶��̴߳�ͬһ�ļ����л������� */
  253. static int OpenLog_OpenFile( LOG *g , char *log_pathfilename , void **open_handle )
  254. {
  255. long l ;
  256. if( log_pathfilename[0] == '\0' )
  257. return 0;
  258. if( g->open_flag == 1 )
  259. return 0;
  260. for( l = 0 ; l < LOG_WINOPENFILE_TRYCOUNT ; l++ )
  261. {
  262. g->hFile = CreateFileA( log_pathfilename , GENERIC_WRITE , FILE_SHARE_READ | FILE_SHARE_WRITE , NULL , OPEN_ALWAYS , FILE_ATTRIBUTE_NORMAL , NULL ) ;
  263. if( g->hFile != INVALID_HANDLE_VALUE )
  264. break;
  265. }
  266. if( l >= LOG_WINOPENFILE_TRYCOUNT )
  267. return LOG_RETURN_ERROR_OPENFILE;
  268. g->open_flag = 1 ;
  269. return 0;
  270. }
  271. static int WriteLog_WriteFile( LOG *g , void **open_handle , int log_level , char *buf , long len , long *writelen )
  272. {
  273. BOOL bret ;
  274. if( g->open_flag == 0 )
  275. return -1;
  276. SetFilePointer( g->hFile , 0 , NULL , FILE_END );
  277. bret = WriteFile( g->hFile , buf , len , writelen , NULL ) ;
  278. if( bret != TRUE )
  279. return LOG_RETURN_ERROR_WRITEFILE;
  280. return 0;
  281. }
  282. static int CloseLog_CloseHandle( LOG *g , void **open_handle )
  283. {
  284. if( g->open_flag == 0 )
  285. return 0;
  286. CloseHandle( g->hFile );
  287. g->open_flag = 0 ;
  288. return 0;
  289. }
  290. static int OpenLog_RegisterEventSource( LOG *g , char *log_pathfilename , void **open_handle )
  291. {
  292. if( log_pathfilename[0] == '\0' )
  293. return 0;
  294. if( g->open_flag == 1 )
  295. return 0;
  296. g->hFile = RegisterEventSource( NULL , g->log_pathfilename ) ;
  297. if( g->hFile == NULL )
  298. return LOG_RETURN_ERROR_OPENFILE;
  299. g->open_flag = 1 ;
  300. return 0;
  301. }
  302. static int WriteLog_ReportEvent( LOG *g , void **open_handle , int log_level , char *buf , long len , long *writelen )
  303. {
  304. unsigned short event_log_level ;
  305. char *ptr = NULL ;
  306. if( g->open_flag == 0 )
  307. return -1;
  308. if( log_level == LOG_LEVEL_DEBUG )
  309. event_log_level = EVENTLOG_INFORMATION_TYPE ;
  310. else if( log_level == LOG_LEVEL_INFO )
  311. event_log_level = EVENTLOG_INFORMATION_TYPE ;
  312. else if( log_level == LOG_LEVEL_WARN )
  313. event_log_level = EVENTLOG_WARNING_TYPE ;
  314. else if( log_level == LOG_LEVEL_ERROR )
  315. event_log_level = EVENTLOG_ERROR_TYPE ;
  316. else if( log_level == LOG_LEVEL_FATAL )
  317. event_log_level = EVENTLOG_ERROR_TYPE ;
  318. else
  319. event_log_level = EVENTLOG_ERROR_TYPE ;
  320. ptr = buf ;
  321. ReportEvent( g->hFile , event_log_level , 0 , 0 , NULL , 1 , 0 , & ptr , NULL );
  322. return 0;
  323. }
  324. static long CloseLog_DeregisterEventSource( LOG *g , void **open_handle )
  325. {
  326. if( g->open_flag == 0 )
  327. return 0;
  328. DeregisterEventSource( g->hFile );
  329. g->open_flag = 0 ;
  330. return 0;
  331. }
  332. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  333. static int OpenLog_open( LOG *g , char *log_pathfilename , void **open_handle )
  334. {
  335. if( log_pathfilename[0] == '\0' )
  336. return 0;
  337. if( g->open_flag == 1 )
  338. return 0;
  339. g->fd = open( log_pathfilename , O_CREAT|O_APPEND|O_WRONLY , S_IRWXU|S_IRWXG|S_IRWXO ) ;
  340. if( g->fd == -1 )
  341. return LOG_RETURN_ERROR_OPENFILE;
  342. g->open_flag = 1 ;
  343. return 0;
  344. }
  345. static int CloseLog_close( LOG *g , void **open_handle )
  346. {
  347. if( g->open_flag == 0 )
  348. return 0;
  349. close( g->fd );
  350. g->open_flag = 0 ;
  351. return 0;
  352. }
  353. static int OpenLog_openlog( LOG *g , char *log_pathfilename , void **open_handle )
  354. {
  355. if( log_pathfilename[0] == '\0' )
  356. return 0;
  357. if( g->open_flag == 1 )
  358. return 0;
  359. openlog( g->log_pathfilename , LOG_PID , LOG_USER );
  360. g->open_flag = 1 ;
  361. return 0;
  362. }
  363. static int WriteLog_syslog( LOG *g , void **open_handle , int log_level , char *buf , long len , long *writelen )
  364. {
  365. int syslog_log_level ;
  366. if( g->open_flag == 0 )
  367. return -1;
  368. if( log_level == LOG_LEVEL_DEBUG )
  369. syslog_log_level = LOG_DEBUG ;
  370. else if( log_level == LOG_LEVEL_INFO )
  371. syslog_log_level = LOG_INFO ;
  372. else if( log_level == LOG_LEVEL_WARN )
  373. syslog_log_level = LOG_WARNING ;
  374. else if( log_level == LOG_LEVEL_ERROR )
  375. syslog_log_level = LOG_ERR ;
  376. else if( log_level == LOG_LEVEL_FATAL )
  377. syslog_log_level = LOG_EMERG ;
  378. else
  379. syslog_log_level = LOG_ALERT ;
  380. syslog( syslog_log_level , buf );
  381. (*writelen) = len ;
  382. return 0;
  383. }
  384. static int CloseLog_closelog( LOG *g , void **open_handle )
  385. {
  386. if( g->open_flag == 0 )
  387. return 0;
  388. closelog();
  389. g->open_flag = 0 ;
  390. return 0;
  391. }
  392. #endif
  393. static int WriteLog_write( LOG *g , void **open_handle , int log_level , char *buf , long len , long *writelen )
  394. {
  395. if( g->open_flag == 0 )
  396. return -1;
  397. (*writelen) = WRITE( g->fd , buf , len ) ;
  398. if( (*writelen) == -1 )
  399. return LOG_RETURN_ERROR_WRITEFILE;
  400. return 0;
  401. }
  402. static int ChangeTest_interval( LOG *g , void **test_handle )
  403. {
  404. int nret ;
  405. g->file_change_test_no--;
  406. if( g->file_change_test_no < 1 )
  407. {
  408. struct STAT file_change_stat ;
  409. nret = STAT( g->log_pathfilename , & file_change_stat ) ;
  410. if( ( nret == -1 || file_change_stat.st_size < g->file_change_stat.st_size ) && g->pfuncCloseLogFinally )
  411. {
  412. nret = g->pfuncCloseLogFinally( g , & (g->open_handle) ) ;
  413. if( nret )
  414. return nret;
  415. }
  416. g->file_change_test_no = g->file_change_test_interval ;
  417. }
  418. return 0;
  419. }
  420. int ExpandPathFilename( char *pathfilename , long pathfilename_bufsize )
  421. {
  422. long pathfilename_len ;
  423. char *p1 = NULL , *p2 = NULL ;
  424. char env_key[ MAXLEN_FILENAME + 1 ] ;
  425. long env_key_len ;
  426. char *env_val = NULL ;
  427. long env_val_len ;
  428. pathfilename_len = strlen(pathfilename) ;
  429. p1 = strchr( pathfilename , '$' );
  430. while( p1 )
  431. {
  432. /* չ���������� */ /* expand environment variable */
  433. p2 = strchr( p1 + 1 , '$' ) ;
  434. if( p2 == NULL )
  435. return LOG_RETURN_ERROR_PARAMETER;
  436. memset( env_key , 0x00 , sizeof(env_key) );
  437. env_key_len = p2 - p1 + 1 ;
  438. strncpy( env_key , p1 + 1 , env_key_len - 2 );
  439. env_val = getenv( env_key ) ;
  440. if( env_val == NULL )
  441. return LOG_RETURN_ERROR_PARAMETER;
  442. env_val_len = strlen(env_val) ;
  443. if( pathfilename_len + ( env_val_len - env_key_len ) > pathfilename_bufsize-1 )
  444. return LOG_RETURN_ERROR_PARAMETER;
  445. memmove( p2+1 + ( env_val_len - env_key_len ) , p2+1 , strlen(p2+1) + 1 );
  446. memcpy( p1 , env_val , env_val_len );
  447. pathfilename_len += env_val_len - env_key_len ;
  448. p1 = strchr( p1 + ( env_val_len - env_key_len ) , '$' );
  449. }
  450. return 0;
  451. }
  452. /* ������־��� */ /* set log output */
  453. int SetLogOutput( LOG *g , int output , char *log_pathfilename , funcOpenLog *pfuncOpenLogFirst , funcOpenLog *pfuncOpenLog , funcWriteLog *pfuncWriteLog , funcChangeTest *pfuncChangeTest , funcCloseLog *pfuncCloseLog , funcCloseLog *pfuncCloseLogFinally )
  454. {
  455. char pathfilename[ MAXLEN_FILENAME + 1 ] ;
  456. int nret = 0 ;
  457. if( g == NULL )
  458. return LOG_RETURN_ERROR_PARAMETER;
  459. if( log_pathfilename == NULL || log_pathfilename[0] == '\0' )
  460. {
  461. memset( pathfilename , 0x00 , sizeof(pathfilename) );
  462. }
  463. else
  464. {
  465. memset( pathfilename , 0x00 , sizeof(pathfilename) );
  466. strncpy( pathfilename , log_pathfilename , sizeof(pathfilename)-1 );
  467. if( output != LOG_OUTPUT_CALLBACK )
  468. {
  469. nret = ExpandPathFilename( pathfilename , sizeof(pathfilename) ) ;
  470. if( nret )
  471. return nret;
  472. }
  473. }
  474. if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_SET_OUTPUT_BY_FILENAME ) )
  475. {
  476. if( strncmp( pathfilename , "#stdout#" , 8 ) == 0 )
  477. {
  478. memmove( pathfilename , pathfilename+8 , strlen(pathfilename+8)+1 );
  479. output = LOG_OUTPUT_STDOUT ;
  480. }
  481. else if( strncmp( pathfilename , "#stderr#" , 8 ) == 0 )
  482. {
  483. memmove( pathfilename , pathfilename+8 , strlen(pathfilename+8)+1 );
  484. output = LOG_OUTPUT_STDERR ;
  485. }
  486. else if( strncmp( pathfilename , "#syslog#" , 8 ) == 0 )
  487. {
  488. memmove( pathfilename , pathfilename+8 , strlen(pathfilename+8)+1 );
  489. output = LOG_OUTPUT_SYSLOG ;
  490. }
  491. }
  492. if( output == LOG_OUTPUT_FILE && TEST_ATTRIBUTE( g->log_options , LOG_OPTION_FILENAME_APPEND_DOT_LOG ) )
  493. {
  494. if( strlen(pathfilename) + 4 > sizeof(pathfilename) - 1 )
  495. return LOG_RETURN_ERROR_PARAMETER;
  496. strcat( pathfilename , ".log" );
  497. }
  498. if( g->open_flag == 1 )
  499. {
  500. if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_ONCE ) || TEST_ATTRIBUTE( g->log_options , LOG_OPTION_CHANGE_TEST ) )
  501. {
  502. if( g->pfuncCloseLogFinally )
  503. {
  504. nret = g->pfuncCloseLogFinally( g , & (g->open_handle) ) ;
  505. }
  506. }
  507. }
  508. if( TEST_NOT_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_AND_CLOSE )
  509. && TEST_NOT_ATTRIBUTE( g->log_options , LOG_OPTION_CHANGE_TEST )
  510. && TEST_NOT_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_ONCE ) )
  511. {
  512. g->log_options |= LOG_OPTION_OPEN_DEFAULT ;
  513. }
  514. memset( g->log_pathfilename , 0x00 , sizeof(g->log_pathfilename) );
  515. strncpy( g->log_pathfilename , pathfilename , sizeof(g->log_pathfilename)-1 );
  516. if( output != LOG_OUTPUT_NOSET )
  517. {
  518. g->output = output ;
  519. if( g->output == LOG_OUTPUT_STDOUT )
  520. {
  521. g->fd = STDOUT_HANDLE ;
  522. g->pfuncOpenLogFirst = NULL ;
  523. g->pfuncOpenLog = NULL ;
  524. g->pfuncWriteLog = & WriteLog_write ;
  525. g->pfuncChangeTest = NULL ;
  526. g->pfuncCloseLog = NULL ;
  527. g->pfuncCloseLogFinally = NULL ;
  528. g->log_options &= ~LOG_OPTION_OPEN_AND_CLOSE ;
  529. g->log_options &= ~LOG_OPTION_CHANGE_TEST ;
  530. g->log_options |= LOG_OPTION_OPEN_ONCE ;
  531. g->open_flag = 1 ;
  532. }
  533. else if( output == LOG_OUTPUT_STDERR )
  534. {
  535. g->fd = STDERR_HANDLE ;
  536. g->pfuncOpenLogFirst = NULL ;
  537. g->pfuncOpenLog = NULL ;
  538. g->pfuncWriteLog = & WriteLog_write ;
  539. g->pfuncChangeTest = NULL ;
  540. g->pfuncCloseLog = NULL ;
  541. g->pfuncCloseLogFinally = NULL ;
  542. g->log_options &= ~LOG_OPTION_OPEN_AND_CLOSE ;
  543. g->log_options &= ~LOG_OPTION_CHANGE_TEST ;
  544. g->log_options |= LOG_OPTION_OPEN_ONCE ;
  545. g->open_flag = 1 ;
  546. }
  547. else if( output == LOG_OUTPUT_SYSLOG )
  548. {
  549. #if ( defined _WIN32 )
  550. g->pfuncOpenLogFirst = & OpenLog_RegisterEventSource ;
  551. g->pfuncOpenLog = NULL ;
  552. g->pfuncWriteLog = & WriteLog_ReportEvent ;
  553. g->pfuncChangeTest = NULL ;
  554. g->pfuncCloseLog = NULL ;
  555. g->pfuncCloseLogFinally = & CloseLog_DeregisterEventSource ;
  556. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  557. g->pfuncOpenLogFirst = & OpenLog_openlog ;
  558. g->pfuncOpenLog = NULL ;
  559. g->pfuncWriteLog = & WriteLog_syslog ;
  560. g->pfuncChangeTest = NULL ;
  561. g->pfuncCloseLog = NULL ;
  562. g->pfuncCloseLogFinally = & CloseLog_closelog ;
  563. #endif
  564. g->log_options &= ~LOG_OPTION_OPEN_AND_CLOSE ;
  565. g->log_options &= ~LOG_OPTION_CHANGE_TEST ;
  566. g->log_options |= LOG_OPTION_OPEN_ONCE ;
  567. g->open_flag = 0 ;
  568. }
  569. else if( output == LOG_OUTPUT_FILE )
  570. {
  571. #if ( defined _WIN32 )
  572. if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_ONCE ) || TEST_ATTRIBUTE( g->log_options , LOG_OPTION_CHANGE_TEST ) )
  573. {
  574. g->pfuncOpenLogFirst = & OpenLog_OpenFile ;
  575. g->pfuncOpenLog = NULL ;
  576. g->pfuncWriteLog = & WriteLog_WriteFile ;
  577. g->pfuncChangeTest = & ChangeTest_interval ;
  578. g->pfuncCloseLog = NULL ;
  579. g->pfuncCloseLogFinally = & CloseLog_CloseHandle ;
  580. }
  581. else
  582. {
  583. g->pfuncOpenLogFirst = NULL ;
  584. g->pfuncOpenLog = & OpenLog_OpenFile ;
  585. g->pfuncWriteLog = & WriteLog_WriteFile ;
  586. g->pfuncChangeTest = NULL ;
  587. g->pfuncCloseLog = & CloseLog_CloseHandle ;
  588. g->pfuncCloseLogFinally = NULL ;
  589. }
  590. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  591. if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_ONCE ) || TEST_ATTRIBUTE( g->log_options , LOG_OPTION_CHANGE_TEST ) )
  592. {
  593. g->pfuncOpenLogFirst = & OpenLog_open ;
  594. g->pfuncOpenLog = NULL ;
  595. g->pfuncWriteLog = & WriteLog_write ;
  596. g->pfuncChangeTest = & ChangeTest_interval ;
  597. g->pfuncCloseLog = NULL ;
  598. g->pfuncCloseLogFinally = & CloseLog_close ;
  599. }
  600. else
  601. {
  602. g->pfuncOpenLogFirst = NULL ;
  603. g->pfuncOpenLog = & OpenLog_open ;
  604. g->pfuncWriteLog = & WriteLog_write ;
  605. g->pfuncChangeTest = NULL ;
  606. g->pfuncCloseLog = & CloseLog_close ;
  607. g->pfuncCloseLogFinally = NULL ;
  608. }
  609. #endif
  610. g->open_flag = 0 ;
  611. }
  612. else if( output == LOG_OUTPUT_CALLBACK )
  613. {
  614. SetLogOutputFuncDirectly( g , pfuncOpenLogFirst , pfuncOpenLog , pfuncWriteLog , pfuncChangeTest , pfuncCloseLog , pfuncCloseLogFinally );
  615. g->open_flag = 0 ;
  616. }
  617. }
  618. if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_ONCE ) || TEST_ATTRIBUTE( g->log_options , LOG_OPTION_CHANGE_TEST ) )
  619. {
  620. if( g->pfuncOpenLogFirst )
  621. {
  622. nret = g->pfuncOpenLogFirst( g , g->log_pathfilename , & (g->open_handle) ) ;
  623. if( nret )
  624. return nret;
  625. }
  626. }
  627. return 0;
  628. }
  629. int SetLogOutput2( LOG *g , int output , funcOpenLog *pfuncOpenLogFirst , funcOpenLog *pfuncOpenLog , funcWriteLog *pfuncWriteLog , funcChangeTest *pfuncChangeTest , funcCloseLog *pfuncCloseLog , funcCloseLog *pfuncCloseLogFinally , char *log_pathfilename_format , ... )
  630. {
  631. va_list valist ;
  632. char log_pathfilename[ MAXLEN_FILENAME + 1 ] ;
  633. memset( log_pathfilename , 0x00 , sizeof(log_pathfilename) );
  634. va_start( valist , log_pathfilename_format );
  635. VSNPRINTF( log_pathfilename , sizeof(log_pathfilename)-1 , log_pathfilename_format , valist );
  636. va_end( valist );
  637. return SetLogOutput( g , output , log_pathfilename , pfuncOpenLogFirst , pfuncOpenLog , pfuncWriteLog , pfuncChangeTest , pfuncCloseLog , pfuncCloseLogFinally );
  638. }
  639. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  640. int SetLogOutputG( int output , char *log_pathfilename , funcOpenLog *pfuncOpenLogFirst , funcOpenLog *pfuncOpenLog , funcWriteLog *pfuncWriteLog , funcChangeTest *pfuncChangeTest , funcCloseLog *pfuncCloseLog , funcCloseLog *pfuncCloseLogFinally )
  641. {
  642. return SetLogOutput( tls_g , output , log_pathfilename , pfuncOpenLogFirst , pfuncOpenLog , pfuncWriteLog , pfuncChangeTest , pfuncCloseLog , pfuncCloseLogFinally );
  643. }
  644. #endif
  645. /* ������־�ȼ� */ /* set log level */
  646. int SetLogLevel( LOG *g , int log_level )
  647. {
  648. if( g == NULL )
  649. return LOG_RETURN_ERROR_PARAMETER;
  650. g->log_level = log_level ;
  651. return 0;
  652. }
  653. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  654. int SetLogLevelG( int log_level )
  655. {
  656. return SetLogLevel( tls_g , log_level );
  657. }
  658. #endif
  659. /* �и�ʽ�������� */ /* log style functions */
  660. static int LogStyle_SEPARATOR( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  661. {
  662. MemcatLogBuffer( g , logbuf , " | " , 3 );
  663. return 0;
  664. }
  665. static int LogStyle_SEPARATOR2( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  666. {
  667. MemcatLogBuffer( g , logbuf , "| " , 2 );
  668. return 0;
  669. }
  670. static int LogStyle_DATE( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  671. {
  672. if( g->cache1_tv.tv_sec == 0 )
  673. g->cache1_tv.tv_sec = time( NULL ) ;
  674. if( g->cache1_tv.tv_sec != g->cache2_logstyle_tv.tv_sec )
  675. {
  676. LOCALTIME( g->cache1_tv.tv_sec , g->cache1_stime )
  677. FormatLogBuffer( g , logbuf , "%04d-%02d-%02d" , g->cache1_stime.tm_year+1900 , g->cache1_stime.tm_mon+1 , g->cache1_stime.tm_mday );
  678. g->cache2_logstyle_tv.tv_sec = g->cache1_tv.tv_sec ;
  679. g->cache2_logstyle_tv.tv_usec = g->cache1_tv.tv_usec ;
  680. g->cache2_logstyle_date_buf_len = 10 ;
  681. memcpy( g->cache2_logstyle_date_buf , logbuf->bufptr - g->cache2_logstyle_date_buf_len , g->cache2_logstyle_date_buf_len );
  682. g->cache2_logstyle_date_buf[g->cache2_logstyle_date_buf_len] = '\0' ;
  683. }
  684. else
  685. {
  686. MemcatLogBuffer( g , logbuf , g->cache2_logstyle_date_buf , g->cache2_logstyle_date_buf_len );
  687. }
  688. return 0;
  689. }
  690. static int LogStyle_DATETIME( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  691. {
  692. if( g->cache1_tv.tv_sec == 0 )
  693. g->cache1_tv.tv_sec = time( NULL ) ;
  694. if( g->cache1_tv.tv_sec != g->cache2_logstyle_tv.tv_sec )
  695. {
  696. LOCALTIME( g->cache1_tv.tv_sec , g->cache1_stime )
  697. FormatLogBuffer( g , logbuf , "%04d-%02d-%02d %02d:%02d:%02d" , g->cache1_stime.tm_year+1900 , g->cache1_stime.tm_mon+1 , g->cache1_stime.tm_mday , g->cache1_stime.tm_hour , g->cache1_stime.tm_min , g->cache1_stime.tm_sec ) ;
  698. g->cache2_logstyle_tv.tv_sec = g->cache1_tv.tv_sec ;
  699. g->cache2_logstyle_datetime_buf_len = 19 ;
  700. memcpy( g->cache2_logstyle_datetime_buf , logbuf->bufptr - g->cache2_logstyle_datetime_buf_len , g->cache2_logstyle_datetime_buf_len ); g->cache2_logstyle_datetime_buf[g->cache2_logstyle_datetime_buf_len] = '\0' ;
  701. }
  702. else
  703. {
  704. MemcatLogBuffer( g , logbuf , g->cache2_logstyle_datetime_buf , g->cache2_logstyle_datetime_buf_len ) ;
  705. }
  706. return 0;
  707. }
  708. static int LogStyle_DATETIMEMS( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  709. {
  710. #if ( defined _WIN32 )
  711. SYSTEMTIME stNow ;
  712. GetLocalTime( & stNow );
  713. SYSTEMTIME2TIMEVAL_USEC( stNow , g->cache1_tv );
  714. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  715. gettimeofday( & (g->cache1_tv) , NULL );
  716. #endif
  717. LogStyle_DATETIME( g , logbuf , c_filename , c_fileline , log_level , format , valist );
  718. FormatLogBuffer( g , logbuf , ".%06ld" , g->cache1_tv.tv_usec ) ;
  719. return 0;
  720. }
  721. static int LogStyle_LOGLEVEL( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  722. {
  723. FormatLogBuffer( g , logbuf , "%-5s" , sg_aszLogLevelDesc[log_level] );
  724. return 0;
  725. }
  726. static int LogStyle_CUSTLABEL1( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  727. {
  728. FormatLogBuffer( g , logbuf , "%s" , g->cust_label[1-1] );
  729. return 0;
  730. }
  731. static int LogStyle_CUSTLABEL2( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  732. {
  733. FormatLogBuffer( g , logbuf , "%s" , g->cust_label[2-1] );
  734. return 0;
  735. }
  736. static int LogStyle_CUSTLABEL3( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  737. {
  738. FormatLogBuffer( g , logbuf , "%s" , g->cust_label[3-1] );
  739. return 0;
  740. }
  741. static int LogStyle_SPACE( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  742. {
  743. MemcatLogBuffer( g , logbuf , " " , 1 );
  744. return 0;
  745. }
  746. static int LogStyle_PID( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  747. {
  748. unsigned long pid ;
  749. #if ( defined _WIN32 )
  750. HWND hd ;
  751. DWORD dw ;
  752. hd = GetForegroundWindow() ;
  753. GetWindowThreadProcessId( hd , & dw );
  754. pid = (long)dw ;
  755. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  756. pid = (long)getpid() ;
  757. #endif
  758. if( pid != g->cache2_logstyle_pid )
  759. {
  760. g->cache2_logstyle_pid_buf_len = FormatLogBuffer( g , logbuf , "%u:" , pid ) ;
  761. memcpy( g->cache2_logstyle_pid_buf , logbuf->bufptr - g->cache2_logstyle_pid_buf_len , g->cache2_logstyle_pid_buf_len ); g->cache2_logstyle_pid_buf[g->cache2_logstyle_pid_buf_len] = '\0' ;
  762. g->cache2_logstyle_pid = pid ;
  763. }
  764. else
  765. {
  766. MemcatLogBuffer( g , logbuf , g->cache2_logstyle_pid_buf , g->cache2_logstyle_pid_buf_len ) ;
  767. }
  768. return 0;
  769. }
  770. static int LogStyle_TID( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  771. {
  772. unsigned long tid ;
  773. #if ( defined _WIN32 )
  774. tid = (unsigned long)GetCurrentThreadId() ;
  775. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  776. #if ( defined _PTHREAD_H )
  777. tid = (unsigned long)pthread_self() ;
  778. #else
  779. #endif
  780. #endif
  781. if( tid != g->cache2_logstyle_tid )
  782. {
  783. g->cache2_logstyle_tid_buf_len = FormatLogBuffer( g , logbuf , "%u:" , tid ) ;
  784. memcpy( g->cache2_logstyle_tid_buf , logbuf->bufptr - g->cache2_logstyle_tid_buf_len , g->cache2_logstyle_tid_buf_len ); g->cache2_logstyle_tid_buf[g->cache2_logstyle_tid_buf_len] = '\0' ;
  785. g->cache2_logstyle_tid = tid ;
  786. }
  787. else
  788. {
  789. MemcatLogBuffer( g , logbuf , g->cache2_logstyle_tid_buf , g->cache2_logstyle_tid_buf_len ) ;
  790. }
  791. return 0;
  792. }
  793. static int LogStyle_SOURCE( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  794. {
  795. #if ( defined _WIN32 )
  796. char *pfilename = NULL ;
  797. pfilename = strrchr( c_filename , '\\' ) ;
  798. if( pfilename )
  799. {
  800. pfilename++;
  801. }
  802. else
  803. {
  804. pfilename = strrchr( c_filename , '/' ) ;
  805. if( pfilename )
  806. pfilename++;
  807. else
  808. pfilename = c_filename ;
  809. }
  810. FormatLogBuffer( g , logbuf , "%s:%ld" , pfilename , c_fileline ) ;
  811. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  812. FormatLogBuffer( g , logbuf , "%s:%ld" , c_filename , c_fileline ) ;
  813. #endif
  814. return 0;
  815. }
  816. static int LogStyle_FORMAT( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  817. {
  818. FormatLogBufferV( g , logbuf , format , valist );
  819. return 0;
  820. }
  821. static int LogStyle_NEWLINE( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  822. {
  823. MemcatLogBuffer( g , logbuf , LOG_NEWLINE , LOG_NEWLINE_LEN );
  824. return 0;
  825. }
  826. /* �������־�� */ /* fill log segments */
  827. static int LogStyle_FuncArray( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  828. {
  829. int format_func_index ;
  830. funcLogStyle **ppfuncLogStyle = NULL ;
  831. int nret ;
  832. /* ������ʽ�������� */ /* travel all log style functions */
  833. for( format_func_index = 0 , ppfuncLogStyle = g->pfuncLogStyles ; format_func_index < g->style_func_count ; format_func_index++ , ppfuncLogStyle++ )
  834. {
  835. nret = (*ppfuncLogStyle)( g , logbuf , c_filename , c_fileline , log_level , format , valist ) ;
  836. if( nret )
  837. return nret;
  838. }
  839. return 0;
  840. }
  841. /* ��������־��� */ /* set log styles */
  842. int SetLogStyles( LOG *g , long log_styles , funcLogStyle *pfuncLogStyle )
  843. {
  844. if( g == NULL )
  845. return LOG_RETURN_ERROR_PARAMETER;
  846. g->log_styles = log_styles ;
  847. if( g->log_styles == LOG_STYLE_CALLBACK )
  848. {
  849. SetLogStyleFuncDirectly( g , pfuncLogStyle );
  850. return 0;
  851. }
  852. /* ������������ */
  853. g->style_func_count = 0 ;
  854. if( TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_DATETIMEMS ) )
  855. {
  856. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_DATETIMEMS ;
  857. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_SEPARATOR ;
  858. }
  859. else if( TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_DATETIME ) )
  860. {
  861. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_DATETIME ;
  862. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_SEPARATOR ;
  863. }
  864. else if( TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_DATE ) )
  865. {
  866. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_DATE ;
  867. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_SEPARATOR ;
  868. }
  869. if( TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_LOGLEVEL ) )
  870. {
  871. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_LOGLEVEL ;
  872. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_SEPARATOR ;
  873. }
  874. if( TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_CUSTLABEL1 )
  875. || TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_CUSTLABEL2 )
  876. || TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_CUSTLABEL3 ) )
  877. {
  878. if( TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_CUSTLABEL1 ) )
  879. {
  880. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_CUSTLABEL1 ;
  881. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_SPACE ;
  882. }
  883. if( TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_CUSTLABEL2 ) )
  884. {
  885. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_CUSTLABEL2 ;
  886. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_SPACE ;
  887. }
  888. if( TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_CUSTLABEL3 ) )
  889. {
  890. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_CUSTLABEL3 ;
  891. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_SPACE ;
  892. }
  893. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_SEPARATOR2 ;
  894. }
  895. if( TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_PID )
  896. || TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_TID )
  897. || TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_SOURCE ) )
  898. {
  899. if( TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_PID ) )
  900. {
  901. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_PID ;
  902. }
  903. if( TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_TID ) )
  904. {
  905. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_TID ;
  906. }
  907. if( TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_SOURCE ) )
  908. {
  909. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_SOURCE ;
  910. }
  911. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_SEPARATOR ;
  912. }
  913. if( TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_FORMAT ) )
  914. {
  915. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_FORMAT ;
  916. }
  917. if( TEST_ATTRIBUTE( g->log_styles , LOG_STYLE_NEWLINE ) )
  918. {
  919. g->pfuncLogStyles[ g->style_func_count++ ] = LogStyle_NEWLINE ;
  920. }
  921. g->pfuncLogStyle = & LogStyle_FuncArray ;
  922. return 0;
  923. }
  924. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  925. int SetLogStylesG( long log_styles , funcLogStyle *pfuncLogStyles )
  926. {
  927. return SetLogStyles( tls_g , log_styles , pfuncLogStyles );
  928. }
  929. #endif
  930. /* ת����־�ļ� */ /* rotate log file */
  931. static int RotateLogFileSize( LOG *g , long step )
  932. {
  933. char rotate_log_pathfilename[ MAXLEN_FILENAME + 1 ] ;
  934. char rotate_log_pathfilename_access[ MAXLEN_FILENAME + 1 ] ;
  935. int nret ;
  936. if( g->open_flag == 1 )
  937. {
  938. if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_ONCE ) || TEST_ATTRIBUTE( g->log_options , LOG_OPTION_CHANGE_TEST ) )
  939. {
  940. if( g->pfuncCloseLogFinally )
  941. {
  942. nret = g->pfuncCloseLogFinally( g , & (g->open_handle) ) ;
  943. if( nret )
  944. return nret;
  945. }
  946. }
  947. }
  948. nret = EnterMutexSection( g ) ;
  949. if( nret )
  950. return nret;
  951. nret = STAT( g->log_pathfilename , & (g->file_change_stat) ) ;
  952. if( nret == -1 )
  953. {
  954. LeaveMutexSection( g );
  955. return LOG_RETURN_ERROR_INTERNAL;
  956. }
  957. if( g->file_change_stat.st_size >= g->log_rotate_size )
  958. {
  959. for( g->rotate_file_no = 1 ; g->rotate_file_no <= g->rotate_file_count ; g->rotate_file_no++ )
  960. {
  961. SNPRINTF( rotate_log_pathfilename , sizeof(rotate_log_pathfilename) , "%s.%ld" , g->log_pathfilename , g->rotate_file_no );
  962. strcpy( rotate_log_pathfilename_access , rotate_log_pathfilename );
  963. if( g->pfuncBeforeRotateFile )
  964. g->pfuncBeforeRotateFile( g , rotate_log_pathfilename_access );
  965. nret = ACCESS( rotate_log_pathfilename_access , ACCESS_OK ) ;
  966. if( nret == -1 )
  967. break;
  968. }
  969. if( g->rotate_file_no > g->rotate_file_count )
  970. {
  971. g->rotate_file_no = g->rotate_file_count ;
  972. SNPRINTF( rotate_log_pathfilename , sizeof(rotate_log_pathfilename) , "%s.%ld" , g->log_pathfilename , g->rotate_file_count );
  973. }
  974. nret = RENAME( g->log_pathfilename , rotate_log_pathfilename ) ;
  975. if( nret )
  976. {
  977. UNLINK( rotate_log_pathfilename );
  978. RENAME( g->log_pathfilename , rotate_log_pathfilename ) ;
  979. }
  980. if( g->pfuncAfterRotateFile )
  981. g->pfuncAfterRotateFile( g , rotate_log_pathfilename );
  982. g->skip_count = 1 ;
  983. }
  984. else
  985. {
  986. if( step == 0 )
  987. step = 1 ;
  988. if( g->pressure_factor == 0 )
  989. g->skip_count = 1 ;
  990. else
  991. g->skip_count = ( g->log_rotate_size - g->file_change_stat.st_size ) / step / g->pressure_factor + 1 ;
  992. }
  993. LeaveMutexSection( g );
  994. return 0;
  995. }
  996. static int RotateLogFilePerDate( LOG *g )
  997. {
  998. char rotate_log_pathfilename[ MAXLEN_FILENAME + 1 ] ;
  999. int nret ;
  1000. if( g->open_flag == 1 )
  1001. {
  1002. if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_ONCE ) || TEST_ATTRIBUTE( g->log_options , LOG_OPTION_CHANGE_TEST ) )
  1003. {
  1004. if( g->pfuncCloseLogFinally )
  1005. {
  1006. nret = g->pfuncCloseLogFinally( g , & (g->open_handle) ) ;
  1007. if( nret )
  1008. return nret;
  1009. g->open_flag = 0 ;
  1010. }
  1011. }
  1012. }
  1013. nret = EnterMutexSection( g ) ;
  1014. if( nret )
  1015. return nret;
  1016. if( g->cache1_tv.tv_sec == 0 )
  1017. g->cache1_tv.tv_sec = time( NULL ) ;
  1018. if( g->cache1_stime.tm_mday == 0 )
  1019. LOCALTIME( g->cache1_tv.tv_sec , g->cache1_stime )
  1020. if( g->cache2_rotate_tv.tv_sec == 0 || g->cache2_rotate_stime.tm_mday == 0 )
  1021. {
  1022. memcpy( & (g->cache2_rotate_tv) , & (g->cache1_tv) , sizeof(struct timeval) );
  1023. memcpy( & (g->cache2_rotate_stime) , & (g->cache1_stime) , sizeof(struct tm) );
  1024. }
  1025. else if
  1026. (
  1027. g->cache1_tv.tv_sec == g->cache2_rotate_tv.tv_sec
  1028. ||
  1029. (
  1030. g->cache1_stime.tm_year == g->cache2_rotate_stime.tm_year
  1031. &&
  1032. g->cache1_stime.tm_mon == g->cache2_rotate_stime.tm_mon
  1033. &&
  1034. g->cache1_stime.tm_mday == g->cache2_rotate_stime.tm_mday
  1035. )
  1036. )
  1037. {
  1038. }
  1039. else
  1040. {
  1041. SNPRINTF( rotate_log_pathfilename , sizeof(rotate_log_pathfilename) , "%s.%04d%02d%02d" , g->log_pathfilename , g->cache2_rotate_stime.tm_year+1900 , g->cache2_rotate_stime.tm_mon+1 , g->cache2_rotate_stime.tm_mday );
  1042. RENAME( g->log_pathfilename , rotate_log_pathfilename );
  1043. if( g->pfuncAfterRotateFile )
  1044. g->pfuncAfterRotateFile( g , rotate_log_pathfilename );
  1045. memcpy( & (g->cache2_rotate_tv) , & (g->cache1_tv) , sizeof(struct timeval) );
  1046. memcpy( & (g->cache2_rotate_stime) , & (g->cache1_stime) , sizeof(struct tm) );
  1047. }
  1048. LeaveMutexSection( g );
  1049. return 0;
  1050. }
  1051. static int RotateLogFilePerHour( LOG *g )
  1052. {
  1053. char rotate_log_pathfilename[ MAXLEN_FILENAME + 1 ] ;
  1054. int nret ;
  1055. if( g->open_flag == 1 )
  1056. {
  1057. if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_ONCE ) || TEST_ATTRIBUTE( g->log_options , LOG_OPTION_CHANGE_TEST ) )
  1058. {
  1059. if( g->pfuncCloseLogFinally )
  1060. {
  1061. nret = g->pfuncCloseLogFinally( g , & (g->open_handle) ) ;
  1062. if( nret )
  1063. return nret;
  1064. g->open_flag = 0 ;
  1065. }
  1066. }
  1067. }
  1068. nret = EnterMutexSection( g ) ;
  1069. if( nret )
  1070. return nret;
  1071. if( g->cache1_tv.tv_sec == 0 )
  1072. g->cache1_tv.tv_sec = time( NULL ) ;
  1073. if( g->cache1_stime.tm_mday == 0 )
  1074. LOCALTIME( g->cache1_tv.tv_sec , g->cache1_stime )
  1075. if( g->cache2_rotate_tv.tv_sec == 0 || g->cache2_rotate_stime.tm_mday == 0 )
  1076. {
  1077. memcpy( & (g->cache2_rotate_tv) , & (g->cache1_tv) , sizeof(struct timeval) );
  1078. memcpy( & (g->cache2_rotate_stime) , & (g->cache1_stime) , sizeof(struct tm) );
  1079. }
  1080. else if
  1081. (
  1082. g->cache2_rotate_tv.tv_sec == g->cache1_tv.tv_sec
  1083. ||
  1084. (
  1085. g->cache2_rotate_stime.tm_year == g->cache1_stime.tm_year
  1086. &&
  1087. g->cache2_rotate_stime.tm_mon == g->cache1_stime.tm_mon
  1088. &&
  1089. g->cache2_rotate_stime.tm_mday == g->cache1_stime.tm_mday
  1090. &&
  1091. g->cache2_rotate_stime.tm_hour == g->cache1_stime.tm_hour
  1092. )
  1093. )
  1094. {
  1095. }
  1096. else
  1097. {
  1098. SNPRINTF( rotate_log_pathfilename , sizeof(rotate_log_pathfilename) , "%s.%04d%02d%02d_%02d" , g->log_pathfilename , g->cache2_rotate_stime.tm_year+1900 , g->cache2_rotate_stime.tm_mon+1 , g->cache2_rotate_stime.tm_mday , g->cache2_rotate_stime.tm_hour );
  1099. RENAME( g->log_pathfilename , rotate_log_pathfilename );
  1100. if( g->pfuncAfterRotateFile )
  1101. g->pfuncAfterRotateFile( g , rotate_log_pathfilename );
  1102. memcpy( & (g->cache2_rotate_tv) , & (g->cache1_tv) , sizeof(struct timeval) );
  1103. memcpy( & (g->cache2_rotate_stime) , & (g->cache1_stime) , sizeof(struct tm) );
  1104. }
  1105. LeaveMutexSection( g );
  1106. return 0;
  1107. }
  1108. /* д��־������ */ /* output log */
  1109. int WriteLogBase( LOG *g , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist )
  1110. {
  1111. long writelen ;
  1112. int nret ;
  1113. if( format == NULL )
  1114. return 0;
  1115. /* ��ʼ������־������ */ /* initialize log buffer */
  1116. g->logbuf.buf_remain_len = g->logbuf.buf_size - 1 - 1 ;
  1117. g->logbuf.bufptr = g->logbuf.bufbase ;
  1118. /* �������־������ */ /* fill log buffer */
  1119. if( g->pfuncLogStyle )
  1120. {
  1121. nret = g->pfuncLogStyle( g , & (g->logbuf) , c_filename , c_fileline , log_level , format , valist ) ;
  1122. if( nret )
  1123. return nret;
  1124. }
  1125. /* �Զ��������־ */ /* filter log */
  1126. if( g->pfuncFilterLog )
  1127. {
  1128. nret = g->pfuncFilterLog( g , & (g->open_handle) , log_level , g->logbuf.bufbase , g->logbuf.buf_size-1-1 - g->logbuf.buf_remain_len ) ;
  1129. if( nret )
  1130. return nret;
  1131. }
  1132. /* ���ļ� */ /* open log */
  1133. if( g->open_flag == 0 )
  1134. {
  1135. if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_CHANGE_TEST ) || TEST_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_ONCE ) )
  1136. {
  1137. /* ����־�ļ� */
  1138. if( g->pfuncOpenLogFirst )
  1139. {
  1140. nret = g->pfuncOpenLogFirst( g , g->log_pathfilename , & (g->open_handle) ) ;
  1141. if( nret )
  1142. return nret;
  1143. }
  1144. }
  1145. else if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_AND_CLOSE ) )
  1146. {
  1147. /* ����־�ļ� */
  1148. if( g->pfuncOpenLog )
  1149. {
  1150. nret = g->pfuncOpenLog( g , g->log_pathfilename , & (g->open_handle) ) ;
  1151. if( nret )
  1152. return nret;
  1153. }
  1154. }
  1155. }
  1156. /* ������־������ */ /* output log */
  1157. if( g->pfuncWriteLog )
  1158. {
  1159. nret = g->pfuncWriteLog( g , & (g->open_handle) , log_level , g->logbuf.bufbase , g->logbuf.buf_size-1-1 - g->logbuf.buf_remain_len , & writelen ) ;
  1160. if( nret )
  1161. return nret;
  1162. }
  1163. #if ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  1164. if( g->output == LOG_OUTPUT_FILE )
  1165. {
  1166. g->fsync_elapse--;
  1167. if( g->fsync_elapse < 1 )
  1168. {
  1169. fsync( g->fd );
  1170. g->fsync_elapse = g->fsync_period ;
  1171. }
  1172. }
  1173. #endif
  1174. /* �ر���־ */ /* close log */
  1175. if( g->open_flag == 1 )
  1176. {
  1177. if( g->output == LOG_OUTPUT_FILE || g->output == LOG_OUTPUT_CALLBACK )
  1178. {
  1179. if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_CHANGE_TEST ) )
  1180. {
  1181. /* ������־�ļ� */
  1182. if( g->pfuncChangeTest )
  1183. {
  1184. nret = g->pfuncChangeTest( g , & (g->test_handle) ) ;
  1185. if( nret )
  1186. return nret;
  1187. }
  1188. }
  1189. else if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_AND_CLOSE ) )
  1190. {
  1191. /* �ر���־�ļ� */
  1192. if( g->pfuncCloseLog )
  1193. {
  1194. nret = g->pfuncCloseLog( g , & (g->open_handle) ) ;
  1195. if( nret )
  1196. return nret;
  1197. }
  1198. }
  1199. }
  1200. }
  1201. /* ���������ļ� */
  1202. if( g->output == LOG_OUTPUT_FILE )
  1203. {
  1204. /* ��־ת����� */ /* rotate log file */
  1205. if( g->rotate_mode == LOG_ROTATEMODE_NONE )
  1206. {
  1207. }
  1208. else if( g->rotate_mode == LOG_ROTATEMODE_SIZE && g->log_rotate_size > 0 )
  1209. {
  1210. g->skip_count--;
  1211. if( g->skip_count < 1 )
  1212. {
  1213. RotateLogFileSize( g , writelen );
  1214. }
  1215. }
  1216. else if( g->rotate_mode == LOG_ROTATEMODE_PER_DAY )
  1217. {
  1218. RotateLogFilePerDate( g );
  1219. }
  1220. else if( g->rotate_mode == LOG_ROTATEMODE_PER_HOUR )
  1221. {
  1222. RotateLogFilePerHour( g );
  1223. }
  1224. }
  1225. /* ���һ������ */ /* clean level 1 cache */
  1226. g->cache1_tv.tv_sec = 0 ;
  1227. g->cache1_stime.tm_mday = 0 ;
  1228. return 0;
  1229. }
  1230. /* ����� */ /* code macros */
  1231. #define WRITELOGBASE(_g_,_log_level_) \
  1232. va_list valist; \
  1233. int nret ; \
  1234. if( (_g_) == NULL ) \
  1235. return LOG_RETURN_ERROR_PARAMETER; \
  1236. if( (_g_)->output == LOG_OUTPUT_FILE && (_g_)->log_pathfilename[0] == '\0' ) \
  1237. return 0; \
  1238. if( TEST_LOGLEVEL_NOTENOUGH( _log_level_ , (_g_) ) ) \
  1239. return 0; \
  1240. va_start( valist , format ); \
  1241. nret = WriteLogBase( (_g_) , c_filename , c_fileline , _log_level_ , format , valist ) ; \
  1242. va_end( valist ); \
  1243. if( nret < 0 ) \
  1244. return nret;
  1245. /* ����־�ȼ���д��־ */ /* write log */
  1246. int WriteLog( LOG *g , char *c_filename , long c_fileline , int log_level , char *format , ... )
  1247. {
  1248. WRITELOGBASE( g , log_level )
  1249. return 0;
  1250. }
  1251. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1252. int WriteLogG( char *c_filename , long c_fileline , int log_level , char *format , ... )
  1253. {
  1254. WRITELOGBASE( tls_g , log_level )
  1255. return 0;
  1256. }
  1257. #endif
  1258. /* д������־ */ /* write debug log */
  1259. int DebugLog( LOG *g , const char *c_filename , long c_fileline , const char *format , ... )
  1260. {
  1261. WRITELOGBASE( g , LOG_LEVEL_DEBUG )
  1262. return 0;
  1263. }
  1264. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1265. int DebugLogG( char *c_filename , long c_fileline , char *format , ... )
  1266. {
  1267. WRITELOGBASE( tls_g , LOG_LEVEL_DEBUG )
  1268. return 0;
  1269. }
  1270. #endif
  1271. /* д��ͨ��Ϣ��־ */ /* write info log */
  1272. int InfoLog( LOG *g , const char *c_filename , long c_fileline , const char *format , ... )
  1273. {
  1274. WRITELOGBASE( g , LOG_LEVEL_INFO )
  1275. return 0;
  1276. }
  1277. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1278. int InfoLogG( char *c_filename , long c_fileline , char *format , ... )
  1279. {
  1280. WRITELOGBASE( tls_g , LOG_LEVEL_INFO )
  1281. return 0;
  1282. }
  1283. #endif
  1284. /* д������־ */ /* write warn log */
  1285. int WarnLog( LOG *g , const char *c_filename , long c_fileline , const char *format , ... )
  1286. {
  1287. WRITELOGBASE( g , LOG_LEVEL_WARN )
  1288. return 0;
  1289. }
  1290. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1291. int WarnLogG( char *c_filename , long c_fileline , char *format , ... )
  1292. {
  1293. WRITELOGBASE( tls_g , LOG_LEVEL_WARN )
  1294. return 0;
  1295. }
  1296. #endif
  1297. /* д������־ */ /* write error log */
  1298. int ErrorLog( LOG *g , const char *c_filename , long c_fileline , const char *format , ... )
  1299. {
  1300. WRITELOGBASE( g , LOG_LEVEL_ERROR )
  1301. return 0;
  1302. }
  1303. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1304. int ErrorLogG( char *c_filename , long c_fileline , char *format , ... )
  1305. {
  1306. WRITELOGBASE( tls_g , LOG_LEVEL_ERROR )
  1307. return 0;
  1308. }
  1309. #endif
  1310. /* д����������־ */ /* write fatal log */
  1311. int FatalLog( LOG *g , const char *c_filename , long c_fileline , const char *format , ... )
  1312. {
  1313. WRITELOGBASE( g , LOG_LEVEL_FATAL )
  1314. return 0;
  1315. }
  1316. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1317. int FatalLogG( char *c_filename , long c_fileline , char *format , ... )
  1318. {
  1319. WRITELOGBASE( tls_g , LOG_LEVEL_FATAL )
  1320. return 0;
  1321. }
  1322. #endif
  1323. /* дʮ�����ƿ���־������ */ /* output log */
  1324. int WriteHexLogBase( LOG *g , char *c_filename , long c_fileline , int log_level , char *buffer , long buflen , char *format , va_list valist )
  1325. {
  1326. int row_offset , col_offset ;
  1327. long len ;
  1328. long writelen ;
  1329. int nret ;
  1330. /* ��ʼ��ʮ�����ƿ���־������ */ /* initialize log buffer */
  1331. g->hexlogbuf.buf_remain_len = g->hexlogbuf.buf_size - 1 - 1 ;
  1332. g->hexlogbuf.bufptr = g->hexlogbuf.bufbase ;
  1333. /* �������־������ */ /* fill log buffer */
  1334. if( format )
  1335. {
  1336. if( g->pfuncLogStyle )
  1337. {
  1338. nret = g->pfuncLogStyle( g , & (g->hexlogbuf) , c_filename , c_fileline , log_level , format , valist ) ;
  1339. if( nret )
  1340. return nret;
  1341. }
  1342. }
  1343. /* ���ʮ�����ƿ���־������ */ /* fill hex log buffer */
  1344. if( buffer && buflen > 0 )
  1345. {
  1346. len = SNPRINTF( g->hexlogbuf.bufptr , g->hexlogbuf.buf_remain_len , " 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF" ) ;
  1347. OFFSET_BUFPTR( & (g->hexlogbuf) , len )
  1348. len = SNPRINTF( g->hexlogbuf.bufptr , g->hexlogbuf.buf_remain_len , LOG_NEWLINE ) ;
  1349. OFFSET_BUFPTR( & (g->hexlogbuf) , len )
  1350. row_offset = 0 ;
  1351. col_offset = 0 ;
  1352. while(1)
  1353. {
  1354. len = SNPRINTF( g->hexlogbuf.bufptr , g->hexlogbuf.buf_remain_len , "0x%08X " , row_offset * 16 ) ;
  1355. OFFSET_BUFPTR_IN_LOOP( & (g->hexlogbuf) , len )
  1356. for( col_offset = 0 ; col_offset < 16 ; col_offset++ )
  1357. {
  1358. if( row_offset * 16 + col_offset < buflen )
  1359. {
  1360. len = SNPRINTF( g->hexlogbuf.bufptr , g->hexlogbuf.buf_remain_len , "%02X " , *((unsigned char *)buffer+row_offset*16+col_offset)) ;
  1361. OFFSET_BUFPTR_IN_LOOP( & (g->hexlogbuf) , len )
  1362. }
  1363. else
  1364. {
  1365. len = SNPRINTF( g->hexlogbuf.bufptr , g->hexlogbuf.buf_remain_len , " " ) ;
  1366. OFFSET_BUFPTR_IN_LOOP( & (g->hexlogbuf) , len )
  1367. }
  1368. }
  1369. len = SNPRINTF( g->hexlogbuf.bufptr , g->hexlogbuf.buf_remain_len , " " ) ;
  1370. OFFSET_BUFPTR_IN_LOOP( & (g->hexlogbuf) , len )
  1371. for( col_offset = 0 ; col_offset < 16 ; col_offset++ )
  1372. {
  1373. if( row_offset * 16 + col_offset < buflen )
  1374. {
  1375. if( isprint( (int)*((unsigned char*)buffer+row_offset*16+col_offset) ) )
  1376. {
  1377. len = SNPRINTF( g->hexlogbuf.bufptr , g->hexlogbuf.buf_remain_len , "%c" , *((unsigned char *)buffer+row_offset*16+col_offset) ) ;
  1378. OFFSET_BUFPTR_IN_LOOP( & (g->hexlogbuf) , len )
  1379. }
  1380. else
  1381. {
  1382. len = SNPRINTF( g->hexlogbuf.bufptr , g->hexlogbuf.buf_remain_len , "." ) ;
  1383. OFFSET_BUFPTR_IN_LOOP( & (g->hexlogbuf) , len )
  1384. }
  1385. }
  1386. else
  1387. {
  1388. len = SNPRINTF( g->hexlogbuf.bufptr , g->hexlogbuf.buf_remain_len , " " ) ;
  1389. OFFSET_BUFPTR_IN_LOOP( & (g->hexlogbuf) , len )
  1390. }
  1391. }
  1392. len = SNPRINTF( g->hexlogbuf.bufptr , g->hexlogbuf.buf_remain_len , LOG_NEWLINE ) ;
  1393. OFFSET_BUFPTR_IN_LOOP( & (g->hexlogbuf) , len )
  1394. if( row_offset * 16 + col_offset >= buflen )
  1395. break;
  1396. row_offset++;
  1397. }
  1398. }
  1399. if( g->hexlogbuf.bufptr == g->hexlogbuf.bufbase )
  1400. return 0;
  1401. if( STRNCMP( g->hexlogbuf.bufptr-(sizeof(LOG_NEWLINE)-1) , != , LOG_NEWLINE , sizeof(LOG_NEWLINE)-1 ) )
  1402. {
  1403. memcpy( g->hexlogbuf.bufptr-(sizeof(LOG_NEWLINE)-1) , LOG_NEWLINE , sizeof(LOG_NEWLINE)-1 );
  1404. }
  1405. /* �Զ��������־ */ /* filter log */
  1406. if( g->pfuncFilterLog )
  1407. {
  1408. nret = g->pfuncFilterLog( g , & (g->open_handle) , log_level , g->hexlogbuf.bufbase , g->hexlogbuf.buf_size-1-1 - g->hexlogbuf.buf_remain_len ) ;
  1409. if( nret )
  1410. return nret;
  1411. }
  1412. /* ���ļ� */ /* open log */
  1413. if( g->open_flag == 0 )
  1414. {
  1415. if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_CHANGE_TEST ) || TEST_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_ONCE ) )
  1416. {
  1417. /* ����־�ļ� */
  1418. if( g->pfuncOpenLogFirst )
  1419. {
  1420. nret = g->pfuncOpenLogFirst( g , g->log_pathfilename , & (g->open_handle) ) ;
  1421. if( nret )
  1422. return nret;
  1423. }
  1424. }
  1425. else if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_AND_CLOSE ) )
  1426. {
  1427. /* ����־�ļ� */
  1428. if( g->pfuncOpenLog )
  1429. {
  1430. nret = g->pfuncOpenLog( g , g->log_pathfilename , & (g->open_handle) ) ;
  1431. if( nret )
  1432. return nret;
  1433. }
  1434. }
  1435. }
  1436. /* ������־������ */ /* output file */
  1437. if( g->pfuncWriteLog )
  1438. {
  1439. nret = g->pfuncWriteLog( g , & (g->open_handle) , log_level , g->hexlogbuf.bufbase , g->hexlogbuf.buf_size-1-1 - g->hexlogbuf.buf_remain_len , & writelen ) ;
  1440. if( nret )
  1441. return nret;
  1442. }
  1443. #if ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  1444. if( g->output == LOG_OUTPUT_FILE )
  1445. {
  1446. g->fsync_elapse--;
  1447. if( g->fsync_elapse < 1 )
  1448. {
  1449. fsync( g->fd );
  1450. g->fsync_elapse = g->fsync_period ;
  1451. }
  1452. }
  1453. #endif
  1454. /* �ر���־ */ /* close file */
  1455. if( g->open_flag == 1 )
  1456. {
  1457. if( g->output == LOG_OUTPUT_FILE || g->output == LOG_OUTPUT_CALLBACK )
  1458. {
  1459. if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_CHANGE_TEST ) )
  1460. {
  1461. /* ������־�ļ� */
  1462. if( g->pfuncChangeTest )
  1463. {
  1464. nret = g->pfuncChangeTest( g , & (g->test_handle) ) ;
  1465. if( nret )
  1466. return nret;
  1467. }
  1468. }
  1469. /*
  1470. else if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_ONCE ) )
  1471. {
  1472. }
  1473. */
  1474. else if( TEST_ATTRIBUTE( g->log_options , LOG_OPTION_OPEN_AND_CLOSE ) )
  1475. {
  1476. /* �ر���־�ļ� */
  1477. if( g->pfuncCloseLog )
  1478. {
  1479. nret = g->pfuncCloseLog( g , & (g->open_handle) ) ;
  1480. if( nret )
  1481. return nret;
  1482. }
  1483. }
  1484. }
  1485. }
  1486. /* ���������ļ� */
  1487. if( g->output == LOG_OUTPUT_FILE )
  1488. {
  1489. /* ��־ת����� */ /* rotate log file */
  1490. if( g->rotate_mode == LOG_ROTATEMODE_NONE )
  1491. {
  1492. }
  1493. else if( g->rotate_mode == LOG_ROTATEMODE_SIZE && g->log_rotate_size > 0 )
  1494. {
  1495. g->skip_count--;
  1496. if( g->skip_count < 1 )
  1497. {
  1498. RotateLogFileSize( g , writelen );
  1499. }
  1500. }
  1501. else if( g->rotate_mode == LOG_ROTATEMODE_PER_DAY )
  1502. {
  1503. RotateLogFilePerDate( g );
  1504. }
  1505. else if( g->rotate_mode == LOG_ROTATEMODE_PER_HOUR )
  1506. {
  1507. RotateLogFilePerHour( g );
  1508. }
  1509. }
  1510. /* ���һ������ */ /* clean level 1 cache */
  1511. g->cache1_tv.tv_sec = 0 ;
  1512. g->cache1_stime.tm_mday = 0 ;
  1513. return 0;
  1514. }
  1515. /* ����� */ /* code macro */
  1516. #define WRITEHEXLOGBASE(_g_,_log_level_) \
  1517. va_list valist; \
  1518. int nret ; \
  1519. if( (_g_) == NULL ) \
  1520. return LOG_RETURN_ERROR_PARAMETER; \
  1521. if( (_g_)->output == LOG_OUTPUT_FILE && (_g_)->log_pathfilename[0] == '\0' ) \
  1522. return 0; \
  1523. if( TEST_LOGLEVEL_NOTENOUGH( _log_level_ , (_g_) ) ) \
  1524. return 0; \
  1525. va_start( valist , format ); \
  1526. nret = WriteHexLogBase( (_g_) , c_filename , c_fileline , _log_level_ , buffer , buflen , format , valist ) ; \
  1527. va_end( valist ); \
  1528. if( nret ) \
  1529. return nret;
  1530. /* ����־�ȼ���дʮ�����ƿ���־ */ /* write hex log */
  1531. int WriteHexLog( LOG *g , char *c_filename , long c_fileline , int log_level , char *buffer , long buflen , char *format , ... )
  1532. {
  1533. WRITEHEXLOGBASE( g , log_level )
  1534. return 0;
  1535. }
  1536. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1537. int WriteHexLogG( char *c_filename , long c_fileline , int log_level , char *buffer , long buflen , char *format , ... )
  1538. {
  1539. WRITEHEXLOGBASE( tls_g , log_level )
  1540. return 0;
  1541. }
  1542. #endif
  1543. /* дʮ�����ƿ������־ */ /* write debug hex log */
  1544. int DebugHexLog( LOG *g , char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... )
  1545. {
  1546. WRITEHEXLOGBASE( g , LOG_LEVEL_DEBUG )
  1547. return 0;
  1548. }
  1549. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1550. int DebugHexLogG( char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... )
  1551. {
  1552. WRITEHEXLOGBASE( tls_g , LOG_LEVEL_DEBUG )
  1553. return 0;
  1554. }
  1555. #endif
  1556. /* дʮ�����ƿ���ͨ��Ϣ��־ */ /* write info hex log */
  1557. int InfoHexLog( LOG *g , char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... )
  1558. {
  1559. WRITEHEXLOGBASE( g , LOG_LEVEL_INFO )
  1560. return 0;
  1561. }
  1562. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1563. int InfoHexLogG( char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... )
  1564. {
  1565. WRITEHEXLOGBASE( tls_g , LOG_LEVEL_INFO )
  1566. return 0;
  1567. }
  1568. #endif
  1569. /* дʮ�����ƿ龯����־ */ /* write warn hex log */
  1570. int WarnHexLog( LOG *g , char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... )
  1571. {
  1572. WRITEHEXLOGBASE( g , LOG_LEVEL_WARN )
  1573. return 0;
  1574. }
  1575. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1576. int WarnHexLogG( char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... )
  1577. {
  1578. WRITEHEXLOGBASE( tls_g , LOG_LEVEL_WARN )
  1579. return 0;
  1580. }
  1581. #endif
  1582. /* дʮ�����ƿ������־ */ /* write error hex log */
  1583. int ErrorHexLog( LOG *g , char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... )
  1584. {
  1585. WRITEHEXLOGBASE( g , LOG_LEVEL_ERROR )
  1586. return 0;
  1587. }
  1588. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1589. int ErrorHexLogG( char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... )
  1590. {
  1591. WRITEHEXLOGBASE( tls_g , LOG_LEVEL_ERROR )
  1592. return 0;
  1593. }
  1594. #endif
  1595. /* дʮ�����ƿ�����������־ */ /* write fatal hex log */
  1596. int FatalHexLog( LOG *g , char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... )
  1597. {
  1598. WRITEHEXLOGBASE( g , LOG_LEVEL_FATAL )
  1599. return 0;
  1600. }
  1601. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1602. int FatalHexLogG( char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... )
  1603. {
  1604. WRITEHEXLOGBASE( tls_g , LOG_LEVEL_FATAL )
  1605. return 0;
  1606. }
  1607. #endif
  1608. /* �ļ��䶯���Լ�� */
  1609. #define LOG_FILECHANGETEST_INTERVAL_ON_OPEN_ONCE_DEFAULT 10
  1610. /* ������־ѡ�� */ /* set log options */
  1611. int SetLogOptions( LOG *g , int log_options )
  1612. {
  1613. if( g == NULL )
  1614. return LOG_RETURN_ERROR_PARAMETER;
  1615. g->log_options = log_options ;
  1616. SetLogFileChangeTest( g , LOG_FILECHANGETEST_INTERVAL_ON_OPEN_ONCE_DEFAULT );
  1617. return 0;
  1618. }
  1619. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1620. int SetLogOptionsG( int log_options )
  1621. {
  1622. return SetLogOptions( tls_g , log_options );
  1623. }
  1624. #endif
  1625. /* �����ļ��ı���Լ�� */
  1626. int SetLogFileChangeTest( LOG *g , long interval )
  1627. {
  1628. if( g == NULL )
  1629. return LOG_RETURN_ERROR_PARAMETER;
  1630. g->file_change_test_interval = interval ;
  1631. g->file_change_test_no = g->file_change_test_interval ;
  1632. memset( & (g->file_change_stat) , 0x00 , sizeof(g->file_change_stat) );
  1633. return 0;
  1634. }
  1635. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1636. int SetLogFileChangeTestG( long interval )
  1637. {
  1638. return SetLogFileChangeTest( tls_g , interval );
  1639. }
  1640. #endif
  1641. /* ˢ�洢IO���� */
  1642. int SetLogFsyncPeriod( LOG *g , long period )
  1643. {
  1644. g->fsync_period = period ;
  1645. g->fsync_elapse = g->fsync_period ;
  1646. return 0;
  1647. }
  1648. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1649. int SetLogFsyncPeriodG( long period )
  1650. {
  1651. return SetLogFsyncPeriod( tls_g , period );
  1652. }
  1653. #endif
  1654. /* ������־�Զ����ǩ */ /* set log custom labels */
  1655. int SetLogCustLabel( LOG *g , int index , char *cust_label )
  1656. {
  1657. if( g == NULL )
  1658. return LOG_RETURN_ERROR_PARAMETER;
  1659. if( 1 <= index && index <= LOG_MAXCNT_CUST_LABEL )
  1660. {
  1661. memset( g->cust_label[index-1] , 0x00 , sizeof(g->cust_label[index-1]) );
  1662. if( cust_label && cust_label[0] != '\0' )
  1663. strncpy( g->cust_label[index-1] , cust_label , sizeof(g->cust_label[index-1])-1 );
  1664. return 0;
  1665. }
  1666. else
  1667. {
  1668. return LOG_RETURN_ERROR_PARAMETER;
  1669. }
  1670. }
  1671. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1672. int SetLogCustLabelG( int index , char *cust_label )
  1673. {
  1674. return SetLogCustLabel( tls_g , index , cust_label );
  1675. }
  1676. #endif
  1677. /* ������־ת��ģʽ */ /* set log rotate mode */
  1678. int SetLogRotateMode( LOG *g , int rotate_mode )
  1679. {
  1680. if( g == NULL )
  1681. return LOG_RETURN_ERROR_PARAMETER;
  1682. if( rotate_mode == LOG_ROTATEMODE_SIZE
  1683. || rotate_mode == LOG_ROTATEMODE_PER_DAY
  1684. || rotate_mode == LOG_ROTATEMODE_PER_HOUR )
  1685. {
  1686. g->rotate_mode = rotate_mode ;
  1687. return 0;
  1688. }
  1689. else
  1690. {
  1691. return LOG_RETURN_ERROR_PARAMETER;
  1692. }
  1693. }
  1694. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1695. int SetLogRotateModeG( int rotate_mode )
  1696. {
  1697. return SetLogRotateMode( tls_g , rotate_mode );
  1698. }
  1699. #endif
  1700. /* ������־ת����С */ /* set rotate size */
  1701. int SetLogRotateSize( LOG *g , long log_rotate_size )
  1702. {
  1703. if( g == NULL )
  1704. return LOG_RETURN_ERROR_PARAMETER;
  1705. if( log_rotate_size <= 0 )
  1706. return LOG_RETURN_ERROR_PARAMETER;
  1707. g->log_rotate_size = log_rotate_size ;
  1708. return 0;
  1709. }
  1710. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1711. int SetLogRotateSizeG( long log_rotate_size )
  1712. {
  1713. return SetLogRotateSize( tls_g , log_rotate_size );
  1714. }
  1715. #endif
  1716. /* ������־ת������ϵ�� */ /* set rotate pressure fator */
  1717. int SetLogRotatePressureFactor( LOG *g , long pressure_factor )
  1718. {
  1719. if( g == NULL )
  1720. return LOG_RETURN_ERROR_PARAMETER;
  1721. if( pressure_factor < 0 )
  1722. return LOG_RETURN_ERROR_PARAMETER;
  1723. g->pressure_factor = pressure_factor ;
  1724. return 0;
  1725. }
  1726. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1727. int SetLogRotatePressureFactorG( long pressure_factor )
  1728. {
  1729. return SetLogRotatePressureFactor( tls_g , pressure_factor );
  1730. }
  1731. #endif
  1732. /* ������־ת������׺��� */
  1733. int SetLogRotateFileCount( LOG *g , long rotate_file_count )
  1734. {
  1735. if( g == NULL )
  1736. return LOG_RETURN_ERROR_PARAMETER;
  1737. if( rotate_file_count <= 0 )
  1738. return LOG_RETURN_ERROR_PARAMETER;
  1739. g->rotate_file_count = rotate_file_count ;
  1740. return 0;
  1741. }
  1742. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1743. int SetLogRotateFileCountG( long rotate_file_count )
  1744. {
  1745. return SetLogRotateFileCount( tls_g , rotate_file_count );
  1746. }
  1747. #endif
  1748. /* �����Զ�����־ת��ǰ�ص����� */ /* set custom callback function before rotate log */
  1749. int SetBeforeRotateFileFunc( LOG *g , funcBeforeRotateFile *pfuncBeforeRotateFile )
  1750. {
  1751. g->pfuncBeforeRotateFile = pfuncBeforeRotateFile ;
  1752. return 0;
  1753. }
  1754. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1755. int SetBeforeRotateFileFuncG( funcBeforeRotateFile *pfuncBeforeRotateFile )
  1756. {
  1757. return SetBeforeRotateFileFunc( tls_g , pfuncBeforeRotateFile );
  1758. }
  1759. #endif
  1760. /* �����Զ�����־ת����ص����� */ /* set custom callback function after rotate log */
  1761. int SetAfterRotateFileFunc( LOG *g , funcAfterRotateFile *pfuncAfterRotateFile )
  1762. {
  1763. g->pfuncAfterRotateFile = pfuncAfterRotateFile ;
  1764. return 0;
  1765. }
  1766. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1767. int SetAfterRotateFileFuncG( funcAfterRotateFile *pfuncAfterRotateFile )
  1768. {
  1769. return SetAfterRotateFileFunc( tls_g , pfuncAfterRotateFile );
  1770. }
  1771. #endif
  1772. /* �����Զ�������־�ȼ��ص��������� */ /* set custom filter callback function */
  1773. int SetFilterLogFunc( LOG *g , funcFilterLog *pfuncFilterLog )
  1774. {
  1775. g->pfuncFilterLog = pfuncFilterLog ;
  1776. return 0;
  1777. }
  1778. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1779. int SetFilterLogFuncG( funcFilterLog *pfuncFilterLog )
  1780. {
  1781. return SetFilterLogFunc( tls_g , pfuncFilterLog );
  1782. }
  1783. #endif
  1784. /* ��������־��������С */ /* set log buffer size */
  1785. int SetLogBufferSize( LOG *g , long log_bufsize , long max_log_bufsize )
  1786. {
  1787. return SetBufferSize( g , & (g->logbuf) , log_bufsize , max_log_bufsize );
  1788. }
  1789. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1790. int SetLogBufferSizeG( long log_bufsize , long max_log_bufsize )
  1791. {
  1792. return SetLogBufferSize( tls_g , log_bufsize , max_log_bufsize );
  1793. }
  1794. #endif
  1795. /* ����ʮ��������־��������С */ /* set hex log buffer size */
  1796. int SetHexLogBufferSize( LOG *g , long hexlog_bufsize , long max_hexlog_bufsize )
  1797. {
  1798. return SetBufferSize( g , & (g->hexlogbuf) , hexlog_bufsize , max_hexlog_bufsize );
  1799. }
  1800. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1801. int SetHexLogBufferSizeG( long hexlog_bufsize , long max_hexlog_bufsize )
  1802. {
  1803. return SetHexLogBufferSize( tls_g , hexlog_bufsize , max_hexlog_bufsize );
  1804. }
  1805. #endif
  1806. /* ֱ��������־����ص����� */
  1807. int SetLogOutputFuncDirectly( LOG *g , funcOpenLog *pfuncOpenLogFirst , funcOpenLog *pfuncOpenLog , funcWriteLog *pfuncWriteLog , funcChangeTest *pfuncChangeTest , funcCloseLog *pfuncCloseLog , funcCloseLog *pfuncCloseLogFinally )
  1808. {
  1809. g->pfuncOpenLogFirst = pfuncOpenLogFirst ;
  1810. g->pfuncOpenLog = pfuncOpenLog ;
  1811. g->pfuncWriteLog = pfuncWriteLog ;
  1812. g->pfuncChangeTest = pfuncChangeTest ;
  1813. g->pfuncCloseLog = pfuncCloseLog ;
  1814. g->pfuncCloseLogFinally = pfuncCloseLogFinally ;
  1815. return 0;
  1816. }
  1817. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1818. int SetLogOutputFuncDirectlyG( funcOpenLog *pfuncOpenLogFirst , funcOpenLog *pfuncOpenLog , funcWriteLog *pfuncWriteLog , funcChangeTest *pfuncChangeTest , funcCloseLog *pfuncCloseLog , funcCloseLog *pfuncCloseLogFinally )
  1819. {
  1820. return SetLogOutputFuncDirectly( tls_g , pfuncOpenLogFirst , pfuncOpenLog , pfuncWriteLog , pfuncChangeTest , pfuncCloseLog , pfuncCloseLogFinally );
  1821. }
  1822. #endif
  1823. /* ֱ����������־���ص����� */
  1824. int SetLogStyleFuncDirectly( LOG *g , funcLogStyle *pfuncLogStyle )
  1825. {
  1826. g->pfuncLogStyle = pfuncLogStyle ;
  1827. return 0;
  1828. }
  1829. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1830. int SetLogStyleFuncDirectlyG( funcLogStyle *pfuncLogStyle )
  1831. {
  1832. return SetLogStyleFuncDirectly( tls_g , pfuncLogStyle );
  1833. }
  1834. #endif
  1835. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  1836. /* �õ������̱߳��ش洢��ȱʡ��־����ĺ����汾 */
  1837. LOG *GetGlobalLOG()
  1838. {
  1839. return tls_g;
  1840. }
  1841. void SetGlobalLOG( LOG *g )
  1842. {
  1843. tls_g = g ;
  1844. return;
  1845. }
  1846. #endif
  1847. int SetOpenFlag( LOG *g , char open_flag )
  1848. {
  1849. g->open_flag = open_flag ;
  1850. return 0;
  1851. }
  1852. char IsLogOpened( LOG *g )
  1853. {
  1854. return g->open_flag ;
  1855. }
  1856. int GetLogLevel( LOG *g )
  1857. {
  1858. return g->log_level;
  1859. }
  1860. LOGBUF *GetLogBuffer( LOG *g )
  1861. {
  1862. return & (g->logbuf);
  1863. }
  1864. LOGBUF *GetHexLogBuffer( LOG *g )
  1865. {
  1866. return & (g->hexlogbuf);
  1867. }
  1868. long FormatLogBuffer( LOG *g , LOGBUF *logbuf , const char *format , ... )
  1869. {
  1870. va_list valist ;
  1871. long len ;
  1872. _REDO :
  1873. va_start( valist , format );
  1874. len = VSNPRINTF( logbuf->bufptr , logbuf->buf_remain_len , format , valist ) ;
  1875. va_end( valist );
  1876. #if ( defined _WIN32 )
  1877. if( len == -1 )
  1878. #elif ( defined __linux__ ) || ( defined _AIX )
  1879. if( len >= logbuf->buf_remain_len )
  1880. #endif
  1881. {
  1882. long new_buf_size ;
  1883. int nret ;
  1884. if( logbuf->buf_size == logbuf->max_buf_size )
  1885. {
  1886. logbuf->bufptr[0] = '\0' ;
  1887. return LOG_RETURN_ERROR_ALLOC_MAX;
  1888. }
  1889. new_buf_size = logbuf->buf_size * 2 ;
  1890. if( new_buf_size > logbuf->max_buf_size )
  1891. new_buf_size = logbuf->max_buf_size ;
  1892. nret = SetBufferSize( g , logbuf , new_buf_size , -1 );
  1893. if( nret )
  1894. return nret;
  1895. goto _REDO;
  1896. }
  1897. OFFSET_BUFPTR( logbuf , len )
  1898. return len;
  1899. }
  1900. long FormatLogBufferV( LOG *g , LOGBUF *logbuf , const char *format , va_list valist )
  1901. {
  1902. long len ;
  1903. _REDO :
  1904. len = VSNPRINTF( logbuf->bufptr , logbuf->buf_remain_len , format , valist ) ;
  1905. #if ( defined _WIN32 )
  1906. if( len == -1 )
  1907. #elif ( defined __linux__ ) || ( defined _AIX )
  1908. if( len >= logbuf->buf_remain_len )
  1909. #endif
  1910. {
  1911. long new_buf_size ;
  1912. int nret ;
  1913. if( logbuf->buf_size == logbuf->max_buf_size )
  1914. {
  1915. logbuf->bufptr[0] = '\0' ;
  1916. return LOG_RETURN_ERROR_ALLOC_MAX;
  1917. }
  1918. new_buf_size = logbuf->buf_size * 2 ;
  1919. if( new_buf_size > logbuf->max_buf_size )
  1920. new_buf_size = logbuf->max_buf_size ;
  1921. nret = SetBufferSize( g , logbuf , new_buf_size , -1 );
  1922. if( nret )
  1923. return nret;
  1924. goto _REDO;
  1925. }
  1926. OFFSET_BUFPTR( logbuf , len )
  1927. return len;
  1928. }
  1929. long MemcatLogBuffer( LOG *g , LOGBUF *logbuf ,const char *append , long len )
  1930. {
  1931. _REDO :
  1932. if( len > logbuf->buf_remain_len )
  1933. {
  1934. long new_buf_size ;
  1935. int nret ;
  1936. if( logbuf->buf_size == logbuf->max_buf_size )
  1937. return LOG_RETURN_ERROR_ALLOC_MAX;
  1938. new_buf_size = logbuf->buf_size * 2 ;
  1939. if( new_buf_size > logbuf->max_buf_size )
  1940. new_buf_size = logbuf->max_buf_size ;
  1941. nret = SetBufferSize( g , logbuf , new_buf_size , -1 );
  1942. if( nret )
  1943. return nret;
  1944. goto _REDO;
  1945. }
  1946. memcpy( logbuf->bufptr , append , len );
  1947. OFFSET_BUFPTR( logbuf , len )
  1948. logbuf->bufptr[0] = '\0' ;
  1949. return len;
  1950. }
  1951. int ConvertLogOutput_atoi( char *output_desc , int *p_log_output )
  1952. {
  1953. if( strcmp( output_desc , "STDOUT" ) == 0 )
  1954. (*p_log_output) = LOG_OUTPUT_STDOUT ;
  1955. else if( strcmp( output_desc , "STDERR" ) == 0 )
  1956. (*p_log_output) = LOG_OUTPUT_STDERR ;
  1957. else if( strcmp( output_desc , "SYSLOG" ) == 0 )
  1958. (*p_log_output) = LOG_OUTPUT_SYSLOG ;
  1959. else if( strcmp( output_desc , "FILE" ) == 0 )
  1960. (*p_log_output) = LOG_OUTPUT_FILE ;
  1961. else if( strcmp( output_desc , "CALLBACK" ) == 0 )
  1962. (*p_log_output) = LOG_OUTPUT_CALLBACK ;
  1963. else
  1964. return LOG_RETURN_ERROR_PARAMETER;
  1965. return 0;
  1966. }
  1967. int ConvertLogLevel_atoi( char *log_level_desc , int *p_log_level )
  1968. {
  1969. if( strcmp( log_level_desc , "DEBUG" ) == 0 )
  1970. (*p_log_level) = LOG_LEVEL_DEBUG ;
  1971. else if( strcmp( log_level_desc , "INFO" ) == 0 )
  1972. (*p_log_level) = LOG_LEVEL_INFO ;
  1973. else if( strcmp( log_level_desc , "WARN" ) == 0 )
  1974. (*p_log_level) = LOG_LEVEL_WARN ;
  1975. else if( strcmp( log_level_desc , "ERROR" ) == 0 )
  1976. (*p_log_level) = LOG_LEVEL_ERROR ;
  1977. else if( strcmp( log_level_desc , "FATAL" ) == 0 )
  1978. (*p_log_level) = LOG_LEVEL_FATAL ;
  1979. else if( strcmp( log_level_desc , "NOLOG" ) == 0 )
  1980. (*p_log_level) = LOG_LEVEL_NOLOG ;
  1981. else
  1982. return LOG_RETURN_ERROR_PARAMETER;
  1983. return 0;
  1984. }
  1985. int ConvertLogLevel_itoa( int log_level , char **log_level_desc )
  1986. {
  1987. (*log_level_desc) = sg_aszLogLevelDesc[log_level] ;
  1988. return 0;
  1989. }
  1990. int ConvertLogStyle_atol( char *line_style_desc , long *p_line_style )
  1991. {
  1992. if( strcmp( line_style_desc , "DATE" ) == 0 )
  1993. (*p_line_style) = LOG_STYLE_DATE ;
  1994. else if( strcmp( line_style_desc , "DATETIME" ) == 0 )
  1995. (*p_line_style) = LOG_STYLE_DATETIME ;
  1996. else if( strcmp( line_style_desc , "DATETIMEMS" ) == 0 )
  1997. (*p_line_style) = LOG_STYLE_DATETIMEMS ;
  1998. else if( strcmp( line_style_desc , "LOGLEVEL" ) == 0 )
  1999. (*p_line_style) = LOG_STYLE_LOGLEVEL ;
  2000. else if( strcmp( line_style_desc , "PID" ) == 0 )
  2001. (*p_line_style) = LOG_STYLE_PID ;
  2002. else if( strcmp( line_style_desc , "TID" ) == 0 )
  2003. (*p_line_style) = LOG_STYLE_TID ;
  2004. else if( strcmp( line_style_desc , "SOURCE" ) == 0 )
  2005. (*p_line_style) = LOG_STYLE_SOURCE ;
  2006. else if( strcmp( line_style_desc , "FORMAT" ) == 0 )
  2007. (*p_line_style) = LOG_STYLE_FORMAT ;
  2008. else if( strcmp( line_style_desc , "NEWLINE" ) == 0 )
  2009. (*p_line_style) = LOG_STYLE_NEWLINE ;
  2010. else if( strcmp( line_style_desc , "CUSTLABEL1" ) == 0 )
  2011. (*p_line_style) = LOG_STYLE_CUSTLABEL1 ;
  2012. else if( strcmp( line_style_desc , "CUSTLABEL2" ) == 0 )
  2013. (*p_line_style) = LOG_STYLE_CUSTLABEL2 ;
  2014. else if( strcmp( line_style_desc , "CUSTLABEL3" ) == 0 )
  2015. (*p_line_style) = LOG_STYLE_CUSTLABEL3 ;
  2016. else
  2017. return LOG_RETURN_ERROR_PARAMETER;
  2018. return 0;
  2019. }
  2020. int ConvertLogOption_atol( char *log_option_desc , long *p_log_option )
  2021. {
  2022. if( strcmp( log_option_desc , "OPEN_AND_CLOSE" ) == 0 )
  2023. (*p_log_option) = LOG_OPTION_OPEN_AND_CLOSE ;
  2024. else if( strcmp( log_option_desc , "CHANGE_TEST" ) == 0 )
  2025. (*p_log_option) = LOG_OPTION_CHANGE_TEST ;
  2026. else if( strcmp( log_option_desc , "OPEN_ONCE" ) == 0 )
  2027. (*p_log_option) = LOG_OPTION_OPEN_ONCE ;
  2028. else if( strcmp( log_option_desc , "SET_OUTPUT_BY_FILENAME" ) == 0 )
  2029. (*p_log_option) = LOG_OPTION_SET_OUTPUT_BY_FILENAME ;
  2030. else if( strcmp( log_option_desc , "FILENAME_APPEND_DOT_LOG" ) == 0 )
  2031. (*p_log_option) = LOG_OPTION_FILENAME_APPEND_DOT_LOG ;
  2032. else
  2033. return LOG_RETURN_ERROR_PARAMETER;
  2034. return 0;
  2035. }
  2036. int ConvertLogRotateMode_atoi( char *rotate_mode_desc , int *p_rotate_mode )
  2037. {
  2038. if( strcmp( rotate_mode_desc , "NONE" ) == 0 )
  2039. (*p_rotate_mode) = LOG_ROTATEMODE_NONE ;
  2040. else if( strcmp( rotate_mode_desc , "SIZE" ) == 0 )
  2041. (*p_rotate_mode) = LOG_ROTATEMODE_SIZE ;
  2042. else if( strcmp( rotate_mode_desc , "PER_DAY" ) == 0 )
  2043. (*p_rotate_mode) = LOG_ROTATEMODE_PER_DAY ;
  2044. else if( strcmp( rotate_mode_desc , "PER_HOUR" ) == 0 )
  2045. (*p_rotate_mode) = LOG_ROTATEMODE_PER_HOUR ;
  2046. else
  2047. return LOG_RETURN_ERROR_PARAMETER;
  2048. return 0;
  2049. }
  2050. int ConvertBufferSize_atol( char *bufsize_desc , long *p_bufsize )
  2051. {
  2052. long bufsize_desc_len ;
  2053. bufsize_desc_len = strlen(bufsize_desc) ;
  2054. if( bufsize_desc_len > 2 && strcmp( bufsize_desc + bufsize_desc_len - 2 , "GB" ) == 0 )
  2055. (*p_bufsize) = atol(bufsize_desc) * 1024 * 1024 * 1024 ;
  2056. else if( bufsize_desc_len > 2 && strcmp( bufsize_desc + bufsize_desc_len - 2 , "MB" ) == 0 )
  2057. (*p_bufsize) = atol(bufsize_desc) * 1024 * 1024 ;
  2058. else if( bufsize_desc_len > 2 && strcmp( bufsize_desc + bufsize_desc_len - 2 , "KB" ) == 0 )
  2059. (*p_bufsize) = atol(bufsize_desc) * 1024 ;
  2060. else if( bufsize_desc_len > 1 && strcmp( bufsize_desc + bufsize_desc_len - 1 , "B" ) == 0 )
  2061. (*p_bufsize) = atol(bufsize_desc) ;
  2062. else
  2063. (*p_bufsize) = atol(bufsize_desc) ;
  2064. if( (*p_bufsize) == 0 )
  2065. return LOG_RETURN_ERROR_PARAMETER;
  2066. return 0;
  2067. }