Home > Back-end >  Boost the log module not write files and console at the same time
Boost the log module not write files and console at the same time

Time:01-13

I use this log module, write the log file and the console at the same time. No problem to a log file, write the console, write a few lines, behind won't print, log files are still in the writing.

I use a tail -f log. The log complains:
TAIL: the write () failed

 

Void MyLog: : init (const STD: : string& Filename)
{
_filename=filename;
Boost: : filesystem: : path filepath=boost: : filesystem: : complete (_filename);
Boost: : filesystem: : path dir=filepath. Parent_path ();
If (boost: : filesystem: : the exists (dir)==false)
{
Boost: : filesystem: : create_directories (dir);
}
Auto consoleSink=boost: : log: : add_console_log
(STD: : clog,
Keywords: : filter=expr: : attrKeywords: : format=
(
Expr... stream
<"[" & lt; & lt; boost: : log: : trivial: : severity
<"] "& lt; )
);

Auto pSink=boost: : log: : add_file_log
(
Keywords: : open_mode=STD: : ios: : app,//use the append mode
Keywords: : file_name=filename,
Keywords: : rotation_size=_fileSize * 1024 * 1024,
Keywords: : filter=expr: : attrKeywords: : max_files=_fileCount,
Keywords: : time_based_rotation=sinks: : file: : rotation_at_time_point (0, 0, 0),
Keywords: : format=
(
Expr... stream
<"[" & lt; & lt; boost: : log: : trivial: : severity
<"] "& lt; )
);
PSink - & gt; Locked_backend () - & gt; Auto_flush (true);//that the log updated
ConsoleSink - & gt; Locked_backend () - & gt; Auto_flush (true);//that the log updated
PSink - & gt; Imbue (STD: : locale (" zh_CN. Utf-8 "));//localization
ConsoleSink - & gt; Imbue (STD: : locale (" zh_CN. Utf-8 "));//localization
Boost: : log: : add_common_attributes ();
}

CodePudding user response:

To compile the release way to perform normal, also don't know what the devil [face] monkey2:012 PNG [/face]

CodePudding user response:

Why do I set the
Keywords: : max_files=_fileCount,

Control log file number is invalid?

CodePudding user response:

Advice for my this earthen MyLog:
 # include & lt; stdio.h> 
# include & lt; Stdlib. H>
# include & lt; String. H>
# ifdef WIN32
# include & lt; Windows. H>
# include & lt; IO. H>
# include & lt; Process. H>
# define MYVOID void
# the else
# include & lt; Unistd. H>
# include & lt; Sys/time. H>
# include & lt; Pthread. H>
# define CRITICAL_SECTION pthread_mutex_t
# define _vsnprintf vsnprintf
# define MYVOID void *
# endif
//the Log {
# define MAXLOGSIZE 20000000
# define ARRSIZE (x) (sizeof (x)/sizeof (x [0]))
# include & lt; Time. H>
# include & lt; Sys/timeb. H>
# include & lt; Stdarg. H>
Char logfilename1 []="MyLog1. Log";
Char logfilename2 []="MyLog2. Log";
Char logstr [16000].
Char datestr [16].
Char timestr [16].
Char MSS [4].
CRITICAL_SECTION cs_log;
The FILE * flog;
# ifdef WIN32
The Lock (CRITICAL_SECTION void * l) {
The EnterCriticalSection (l);
}
Void Unlock (CRITICAL_SECTION * l) {
LeaveCriticalSection (l);
}
Void sleep_ms (int) ms {
Sleep (ms).
}
# the else
The Lock (CRITICAL_SECTION void * l) {
Pthread_mutex_lock (l);
}
Void Unlock (CRITICAL_SECTION * l) {
Pthread_mutex_unlock (l);
}
Void sleep_ms (int) ms {
Usleep (ms * 1000);
}
# endif
Void LogV (const char * pszFmt, va_list argp) {
* struct tm now;
Struct timeb TB;


If (NULL==pszFmt | | 0==pszFmt [0]) return;
If (1==_vsnprintf (logstr, ARRSIZE (logstr), pszFmt, argp)) logstr [ARRSIZE (logstr) - 1)=0.
Ftime (& amp; TB);
Now=localtime (& amp; TB. Time);
Sprintf (datestr, "% d % % 4 d - 02-02 d", now - & gt; Tm_year + 1900, now - & gt; Tm_mon + 1, now - & gt; Tm_mday);
Sprintf (timestr, "02 02 02 % d: % d: % d", now - & gt; Tm_hour, now - & gt; Tm_min, now - & gt; Tm_sec);
Sprintf (MSS, "% 3 d", 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) & gt; MAXLOGSIZE) {
The fclose (flog);
If (rename (logfilename1 logfilename2)) {
Remove (logfilename2);
Rename (logfilename1 logfilename2);
}
Flog=fopen (logfilename1, "a");
If (NULL==flog) return;
}
The fclose (flog);
}
}
Void the Log (const char * pszFmt,... ) {
Va_list argp;

The Lock (& amp; Cs_log);
Va_start (argp, pszFmt);
LogV (pszFmt argp);
Va_end (argp);
Unlock (& amp; Cs_log);
}
//the Log}
Int No_Loop=0;
MYVOID testThread (void * PCN) {
Int n, I;

N=(int) PCN;
i=0;
While (1) {
Sleep_ms (1000);
The Log (" testThread in % d: I==% ds \ n ", n + + I);
If (i> nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related