C Log System

Source: Internet
Author: User
#include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef WIN32 #include <windows.h> #include <io.h> #else #include <unistd.h> #include <sys/time.h> #include <pthread.h&gt
    ; #define CRITICAL_SECTION pthread_mutex_t #define _VSNPRINTF vsnprintf #endif//log{#define MAXLOGSIZE 200 00000 #define Maxlinsize 16000 #include <time.h> #include <sys/timeb.h> #include <stdarg.h> char logfil
Ename1[]= "MyLog1.log";
Char logfilename2[]= "MyLog2.log";
static Char logstr[maxlinsize+1];
Char datestr[16];
Char timestr[16];
Char mss[4];
Critical_section Cs_log;
FILE *flog; #ifdef WIN32 void Lock (critical_section *l) {entercriticalsection (l);} void Unlock (critical_section *l) {Leavec
Riticalsection (l); } #else void Lock (critical_section *l) {pthread_mutex_lock (l);} void Unlock (critical_section *l) {Pthread_mutex
_unlock (l);
 } #endif void Logv (const char *pszfmt,va_list argp) {   struct TM *now;

    struct TIMEB TB; if (null==pszfmt| |
    0==pszfmt[0]) return;
    _vsnprintf (LOGSTR,MAXLINSIZE,PSZFMT,ARGP);
    Ftime (&AMP;TB);
    Now=localtime (&tb.time);
    sprintf (Datestr, "%04d-%02d-%02d", now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf (Timestr, "%02d:%02d:%02d", Now->tm_hour, Now->tm_min, now->tm_sec);
    sprintf (MSS, "%03d", TB.MILLITM);
    printf ("%s%s.%s%s", datestr,timestr,mss,logstr);
    Flog=fopen (logfilename1, "a");
        if (null!=flog) {fprintf (flog, "%s%s.%s%s", datestr,timestr,mss,logstr);
            if (Ftell (flog) >maxlogsize) {fclose (flog);
                if (rename (logfilename1,logfilename2)) {remove (logfilename2);
            Rename (logfilename1,logfilename2);
        }} else {fclose (flog); }}} void Log (const char *pszfmt,...)

    {va_list ARGP;
    Lock (&cs_log);
    Va_start (ARGP,PSZFMT);
    LOGV (PSZFMT,ARGP); Va_end (ARGP);
Unlock (&cs_log);
    }//log} int main (int argc,char * argv[]) {int i; #ifdef WIN32 initializecriticalsection (&cs_log); #else
Pthread_mutex_init (&cs_log,null);
    #endif for (i=0;i<10000;i++) {log ("The IS-a log%04d from file:%s line:%d\n", I, __file__, __line__);
} #ifdef WIN32 deletecriticalsection (&cs_log);
#else Pthread_mutex_destroy (&cs_log);
#endif return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.