Log.cpp
资源名称:vnc3326s.zip [点击查看]
上传用户:sbftbdw
上传日期:2007-01-03
资源大小:379k
文件大小:3k
源码类别:
远程控制编程
开发平台:
Visual C++
- // Log.cpp: implementation of the Log class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdhdrs.h"
- #include "Log.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- const int Log::ToDebug = 1;
- const int Log::ToFile = 2;
- const int Log::ToConsole = 4;
- const static int LINE_BUFFER_SIZE = 1024;
- Log::Log(int mode, int level, LPSTR filename, bool append)
- {
- m_filename = NULL;
- m_append = false;
- hlogfile = NULL;
- m_todebug = false;
- m_toconsole = false;
- m_tofile = false;
- SetFile(filename, append);
- SetMode(mode);
- }
- void Log::SetMode(int mode) {
- if (mode & ToDebug)
- m_todebug = true;
- else
- m_todebug = false;
- if (mode & ToFile) {
- if (!m_tofile)
- OpenFile();
- } else {
- CloseFile();
- m_tofile = false;
- }
- if (mode & ToConsole) {
- if (!m_toconsole)
- AllocConsole();
- m_toconsole = true;
- } else {
- m_toconsole = false;
- }
- }
- void Log::SetLevel(int level) {
- m_level = level;
- }
- void Log::SetFile(LPSTR filename, bool append)
- {
- CloseFile();
- if (m_filename != NULL)
- free(m_filename);
- m_filename = strdup(filename);
- m_append = append;
- if (m_tofile)
- OpenFile();
- }
- void Log::OpenFile()
- {
- // Is there a file-name?
- if (m_filename == NULL)
- {
- m_todebug = true;
- m_tofile = false;
- Print(0, "Error opening log filen");
- return;
- }
- m_tofile = true;
- // If filename is NULL or invalid we should throw an exception here
- hlogfile = CreateFile(
- m_filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
- OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
- if (hlogfile == NULL) {
- // We should throw an exception here
- m_todebug = true;
- m_tofile = false;
- Print(0, "Error opening log file %sn", m_filename);
- }
- if (m_append) {
- SetFilePointer( hlogfile, 0, NULL, FILE_END );
- } else {
- SetEndOfFile( hlogfile );
- }
- }
- // if a log file is open, close it now.
- void Log::CloseFile() {
- if (hlogfile != NULL) {
- CloseHandle(hlogfile);
- hlogfile = NULL;
- }
- }
- void Log::ReallyPrint(LPSTR format, va_list ap)
- {
- TCHAR line[LINE_BUFFER_SIZE];
- vsprintf(line, format, ap);
- if (m_todebug) OutputDebugString(line);
- if (m_toconsole) {
- DWORD byteswritten;
- WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), line, strlen(line), &byteswritten, NULL);
- };
- if (m_tofile && (hlogfile != NULL)) {
- DWORD byteswritten;
- WriteFile(hlogfile, line, strlen(line), &byteswritten, NULL);
- }
- }
- Log::~Log()
- {
- if (m_filename != NULL)
- free(m_filename);
- CloseFile();
- }