00001 00007 #pragma once 00008 #include <bzscmn/serializer.h> 00009 #include <bzswin/registry.h> 00010 #include <bzscmn/file.h> 00011 00012 DECLARE_SERIALIZEABLE_STRUC4_I(TraceAssistParams, 00013 bool, TraceAssistEnabled, false, 00014 BazisLib::String, MessagePrefix, _T(""), 00015 BazisLib::String, LogFileDirectory, _T(""), 00016 bool, OverwriteFileOnStart, false); 00017 00018 static const TCHAR tszTraceAssistRegPath[] = _T("SOFTWARE\\BazisSoft\\KDVMWare\\TraceAssist"); 00019 00021 class TraceAssistant 00022 { 00023 private: 00024 TraceAssistParams m_Params; 00025 BazisLib::File *m_pLogFile; 00026 BazisLib::FilePath m_LogFileName; 00027 std::string m_Prefix; 00028 00029 public: 00031 void ReloadParams() 00032 { 00033 BazisLib::RegistryKey key(HKEY_LOCAL_MACHINE, tszTraceAssistRegPath); 00034 key.DeserializeObject(m_Params); 00035 m_Prefix = BazisLib::StringToANSIString(m_Params.MessagePrefix); 00036 } 00037 00038 TraceAssistant(LPCTSTR ptszFullPipeName) 00039 : m_LogFileName(_T("")) 00040 , m_pLogFile(NULL) 00041 { 00042 ASSERT(ptszFullPipeName); 00043 LPCTSTR pT = _tcsrchr(ptszFullPipeName, '\\'); 00044 if (pT) 00045 m_LogFileName = (pT + 1 + 3); 00046 else 00047 m_LogFileName = ptszFullPipeName + 3; 00048 m_LogFileName += _T(".log"); 00049 ReloadParams(); 00050 } 00051 00052 ~TraceAssistant() 00053 { 00054 delete m_pLogFile; 00055 } 00056 00058 00063 bool TraceLine(const char *pszLine, size_t LineLength) 00064 { 00065 if (!pszLine || !LineLength) 00066 return false; 00067 if (!m_Params.TraceAssistEnabled || m_Params.LogFileDirectory.empty()) 00068 return false; 00069 if (!m_Prefix.empty()) 00070 if (memcmp(m_Prefix.c_str(), pszLine, m_Prefix.length())) 00071 return false; 00072 if (!m_pLogFile) 00073 { 00074 m_pLogFile = new BazisLib::File(BazisLib::FilePath(m_Params.LogFileDirectory) + m_LogFileName, 00075 BazisLib::FileFlags::ReadWriteAccess, 00076 BazisLib::FileFlags::OpenAlways); 00077 if (!m_pLogFile->Valid()) 00078 { 00079 delete m_pLogFile; 00080 m_pLogFile = NULL; 00081 return false; 00082 } 00083 if (m_Params.OverwriteFileOnStart) 00084 m_pLogFile->Crop(); 00085 else 00086 m_pLogFile->Seek(0, BazisLib::FileFlags::FileEnd); 00087 } 00088 return (m_pLogFile->Write(pszLine, LineLength) == LineLength); 00089 } 00090 };