⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vnclog.cpp

📁 teamviewer source code vc++
💻 CPP
字号:
//  Copyright (C) 2002 RealVNC Ltd. All Rights Reserved.
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
//  USA.
//
//  If the source code for the program is not available from the place from
//  which you received this file, check http://www.teamviewer.com
//  for information on obtaining it.

// Log.cpp: implementation of the VNCLog class.
//
//////////////////////////////////////////////////////////////////////

#include "stdhdrs.h"
#include "vncserver.h"
#include "buildnumber.inc"

extern char buildtime[];
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

const int VNCLog::ToDebug   =  1;
const int VNCLog::ToFile    =  2;
const int VNCLog::ToConsole =  4;

const static int LINE_BUFFER_SIZE = 1024;

VNCLog::VNCLog()
{
	m_lastLogTime = 0;
	m_filename = NULL;
	m_append = false;
    hlogfile = NULL;
    m_todebug = false;
    m_toconsole = false;
    m_tofile = false;
}

void VNCLog::SetMode(int mode)
{
	m_mode = mode;
    if (mode & ToDebug)
        m_todebug = true;
    else
        m_todebug = false;

    if (mode & ToFile)  {
		if (!m_tofile)
			OpenFile();
		// Staudenmeyer: not sure why this is necessary
		m_tofile=true;
	} else {
		CloseFile();
        m_tofile = false;
    }
    
    if (mode & ToConsole) {
        if (!m_toconsole) {
            AllocConsole();
            fclose(stdout);
            fclose(stderr);
#ifdef _MSC_VER
            int fh = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), 0);
            _dup2(fh, 1);
            _dup2(fh, 2);
            _fdopen(1, "wt");
            _fdopen(2, "wt");
            printf("fh is %d\n",fh);
            fflush(stdout);
#endif
        }

        m_toconsole = true;

    } else {
        m_toconsole = false;
    }
}


void VNCLog::SetLevel(int level) {
    m_level = level;
}

void VNCLog::SetFile(const char* filename, bool append) 
{
	if (m_filename != NULL)
		free(m_filename);
	m_filename = strdup(filename);
	m_append = append;
	if (m_tofile)
		OpenFile();
}

void VNCLog::OpenFile()
{
	// Is there a file-name?
	if (m_filename == NULL)
	{
        m_todebug = true;
        m_tofile = false;
        Print(0, "Error opening log file\n");
		return;
	}

    m_tofile  = true;
    
	// If there's an existing log and we're not appending then move it
	if (!m_append)
	{
		// Build the backup filename
		char *backupfilename = new char[strlen(m_filename)+5];
		if (backupfilename)
		{
			strcpy(backupfilename, m_filename);
			strcat(backupfilename, ".bak");
			// Attempt the move and replace any existing backup
			// Note that failure is silent - where would we log a message to? ;)
			MoveFileEx(m_filename, backupfilename, MOVEFILE_REPLACE_EXISTING);
			delete [] backupfilename;
		}
	}

	CloseFile();

    // 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 == INVALID_HANDLE_VALUE) {
        // We should throw an exception here
        m_todebug = true;
        m_tofile = false;
        Print(0, VNCLOG("Error opening log file %s"), m_filename);
    }
    if (m_append) {
        SetFilePointer( hlogfile, 0, NULL, FILE_END );
    } else {
        SetEndOfFile( hlogfile );
    }

	SetLastError(0);							// we don't want the 'file already exists' error logged
	if(m_level == LL_NONE)
	{
		ReallyPrintLine("\r\n\r\n");
		Print(0, VNCLOG("LOGGING STARTED"));
		Print(0, VNCLOG("Version=%s, Buildnumber=%s, Buildtime=%s"), TEAMVIEWER_VERSION, BUILDNUMBER, buildtime);
	}
}

// if a log file is open, close it now.
void VNCLog::CloseFile() {
    if (hlogfile != NULL) {
        CloseHandle(hlogfile);
        hlogfile = NULL;
    }
}

inline void VNCLog::ReallyPrintLine(const char* line) 
{
    if (m_todebug) 
	{
		const int buflen = LINE_BUFFER_SIZE * 2+100;
		char buffer[buflen];
		strcpy(buffer, "TeamViewer: ");
		strncat(buffer, line, buflen-1);
		buffer[buflen-1] = '\0';
		OutputDebugString(buffer);
		SetLastError(0);					// OutputDebugString() may set last error to 2 (file not found)
	};
    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);
    }
}

void VNCLog::ReallyPrint(const char* format, va_list ap) 
{
	// - Write the log message, safely, limiting the output buffer size
	time_t current = time(0);
	TCHAR stime[50];
	TCHAR line[LINE_BUFFER_SIZE * 2+100];
	TCHAR message[LINE_BUFFER_SIZE];
	TCHAR szErrorMsg[LINE_BUFFER_SIZE];

	DWORD  dwErrorCode = GetLastError();
	SetLastError(0);
    
	// remove "year\n" from timestring
	strcpy(stime, ctime(&current));
	stime[19] = '\0';

	// print arguments to string
	_vsnprintf(message, LINE_BUFFER_SIZE, format, ap);

	// remove ending \n or \r\n
	int last = strlen(message)-1;
	if(message[last] == '\n')
		if(last > 0 && message[last-1] == '\r')
			message[last-1] = '\0';
		else
			message[last] = '\0';

	// only print error message if there was an error
	if(dwErrorCode != 0)
	{
		if(FormatMessage( 
			FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwErrorCode,
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
			(char *)&szErrorMsg,
			LINE_BUFFER_SIZE, NULL))
		{
			_snprintf(line, LINE_BUFFER_SIZE * 2 + 100, 
					  "%s: %s -- Error %d: %s", stime+4, message, dwErrorCode, szErrorMsg);
		}
		else
		{
			_snprintf(line, LINE_BUFFER_SIZE * 2 + 100, 
					  "%s: %s -- Error %d\r\n", stime+4, message, dwErrorCode);
		}
	}
	else
		_snprintf(line, LINE_BUFFER_SIZE * 2 + 100, 
			"%s: %s\r\n", stime+4, message);

	ReallyPrintLine(line);
}

VNCLog::~VNCLog()
{
	if (m_filename != NULL)
		free(m_filename);
    CloseFile();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -