Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
Log.cpp
Package: vnc3326s.zip [view]
Upload User: sbftbdw
Upload Date: 2007-01-03
Package Size: 379k
Code Size: 3k
Category:
Remote Control
Development Platform:
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();
- }