- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# Utils.h

Go to the documentation of this file.

    00001
    00015 #pragma once
    00016
    00017 #include "TStd.h"
    00018 #include <boost/lexical_cast.hpp>
    00019 #include <boost/algorithm/string/replace.hpp>
    00020 #include <boost/algorithm/string/find.hpp>
    00021 #include <boost/algorithm/string/erase.hpp>
    00022 #include <boost/algorithm/string/trim.hpp>
    00023
    00024 #define NOMINMAX
    00025 #include <windows.h>
    00026
    00027 #define LOGGING_EXPAND_VARIABLES_STRING_SIZE 1000
    00028 #define LOGGING_PROCESSNAMELENGHT 1024
    00029
    00030 namespace Nvidia {
    00031 namespace Logging {
    00032
    00033 class Utils
    00034 {
    00035 public:
    00037     static long long GetCurrentTimestamp();
    00038
    00040     static long long GetFirstTimestamp();
    00041
    00048     static std::tstring GenerateHumanReadableTypeName(std::tstring typeName);
    00049
    00051     static std::tstring CapitalizeString(std::tstring string);
    00052
    00054     static std::tstring GetProcessName();
    00055
    00057     static std::tstring ExpandEnvironmentVariables(const std::tstring& string);
    00058
    00060     template<typename T>
    00061     static T Maximum(T a, T b)
    00062     {
    00063         return a > b ? a : b;
    00064     }
    00065 };
    00066
    00067 inline long long Utils::GetCurrentTimestamp()
    00068 {
    00069     static long long ignored = GetFirstTimestamp(); //We need that to make sure that the start timestamp stored as a static in GetFirstTimestamp is really first.
    00070     static LARGE_INTEGER frequency;
    00071     static BOOL result = QueryPerformanceFrequency(&frequency);
    00072     LARGE_INTEGER now;
    00073     QueryPerformanceCounter(&now);
    00074     return (now.QuadPart * 1000LL) / frequency.QuadPart;
    00075 }
    00076
    00077 inline long long Utils::GetFirstTimestamp()
    00078 {
    00079     static LARGE_INTEGER frequency;
    00080     static BOOL result = QueryPerformanceFrequency(&frequency);
    00081     static LARGE_INTEGER start;
    00082     static BOOL result2 = QueryPerformanceCounter(&start);
    00083     static long long firstTimestamp = (start.QuadPart * 1000LL) / frequency.QuadPart;
    00084     return firstTimestamp;
    00085 }
    00086
    00087 inline std::tstring Utils::GenerateHumanReadableTypeName(std::tstring typeName)
    00088 {
    00089     boost::algorithm::replace_all(typeName, _T("class"), _T(" "));
    00090     boost::algorithm::replace_all(typeName, _T("*"), _T(" "));
    00091     boost::algorithm::replace_all(typeName, _T("const"), _T(" "));
    00092
    00093     std::tstring::iterator iterator = boost::algorithm::find_first(typeName, _T("<")).begin();
    00094
    00095     if ( iterator != typeName.end() )
    00096     {
    00097         int firstTemplate = (int)(iterator - typeName.begin());
    00098         boost::algorithm::erase_tail(typeName, (int)typeName.size() - firstTemplate);
    00099     }
    00100
    00101     iterator = boost::algorithm::find_last(typeName, _T("::")).begin();
    00102     if ( iterator != typeName.end() )
    00103     {
    00104         int lastNamespace = (int)(iterator - typeName.begin());
    00105         boost::algorithm::erase_head(typeName, lastNamespace + 2);
    00106     }
    00107
    00108     boost::algorithm::trim(typeName);
    00109     return typeName;
    00110 }
    00111
    00112 inline std::tstring Utils::CapitalizeString(std::tstring string)
    00113 {
    00114     std::transform(string.begin(), string.end(), string.begin(), _totupper);
    00115     return string;
    00116 }
    00117
    00118 inline std::tstring Utils::GetProcessName()
    00119 {
    00120     TCHAR processName[LOGGING_PROCESSNAMELENGHT];
    00121     int returnCode = GetModuleFileName(NULL, processName, LOGGING_PROCESSNAMELENGHT);
    00122     if ( returnCode == 0 || returnCode == LOGGING_PROCESSNAMELENGHT )
    00123     {
    00124         throw std::runtime_error("GetModuleFileName error");
    00125     }
    00126
    00127     std::tstring processNameString(processName);
    00128     size_t lastSlash = processNameString.find_last_of(_T('\\'));
    00129     if ( lastSlash != std::tstring::npos )
    00130     {
    00131         boost::algorithm::erase_head(processNameString, (int)(lastSlash + 1));
    00132     }
    00133     return processNameString;
    00134 }
    00135
    00136 inline std::tstring Utils::ExpandEnvironmentVariables(const std::tstring& string)
    00137 {
    00138     TCHAR buffer[LOGGING_EXPAND_VARIABLES_STRING_SIZE];
    00139
    00140     //TODO: http://msdn.microsoft.com/en-us/library/ms724265%28VS.85%29.aspx reports that this doesn't work
    00141     //properly on ANSI builds
    00142     DWORD result = ExpandEnvironmentStrings(string.c_str(), buffer, LOGGING_EXPAND_VARIABLES_STRING_SIZE);
    00143     if ( result == 0 || result >= LOGGING_EXPAND_VARIABLES_STRING_SIZE )
    00144     {
    00145         throw std::runtime_error("ExpandEnvironmentStrings failed");
    00146     }
    00147
    00148     return std::tstring(buffer);
    00149 }
    00150
    00151
    00152
    00153 }
    00154 }