LogFile.cpp
资源名称:视频会议系统.rar [点击查看]
上传用户:popouu88
上传日期:2013-02-11
资源大小:2894k
文件大小:2k
源码类别:
IP电话/视频会议
开发平台:
Visual C++
- // LogFile.cpp: implementation of the CLogFile class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "LogFile.h"
- #include <time.h>
- #include <stdarg.h>
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CLogFile::CLogFile()
- {
- this->fileName = "";
- ::InitializeCriticalSection( &this->session );
- }
- CLogFile::~CLogFile()
- {
- ::DeleteCriticalSection( &this->session );
- }
- void CLogFile::SetFileName( const string filename )
- {
- if( filename.length( ) )
- this->fileName = filename;
- }
- void CLogFile::Write( const char * fmt , ... )
- {
- ::EnterCriticalSection( &this->session );
- try
- {
- string msg = "";
- int d;
- char buf[ 255 ];
- va_list ap;
- va_start( ap , fmt );
- while ( *fmt )
- {
- if (*fmt != '%')
- {
- msg += * fmt++;
- continue;
- }
- switch ( * ++ fmt )
- {
- case 's':
- msg += va_arg( ap , const char * );
- break;
- case 'd':
- d = va_arg( ap , int );
- itoa(d, buf, 10);
- msg += buf;
- break;
- default:
- break;
- }
- fmt++;
- }
- va_end( ap );
- if( ! msg.empty( ) )
- {
- time_t t;
- ::time( & t );
- struct tm * tm = ::localtime( & t );
- char szTime[ 100 ];
- sprintf( szTime , "【%d-%d-%d %d:%d:%d】rn" , tm->tm_year + 1900, tm->tm_mon + 1 , tm->tm_mday , tm->tm_hour , tm->tm_min , tm->tm_sec );
- msg = ( string )szTime + msg + "rn";
- if( ! this->fileName.empty( ) )
- {
- FILE * file = ::fopen( this->fileName.c_str( ) , "a+" );
- if( file )
- {
- ::fwrite( msg.c_str( ) , msg.length( ) , sizeof( char ) , file );
- ::fclose( file );
- }
- }
- printf( msg.c_str( ) );
- }
- }
- catch( ... )
- {
- }
- ::LeaveCriticalSection( &this->session );
- }