00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #pragma once
00020
00021 #include "TestToolBox\CommonSources\CommonDefinitions.h"
00022
00023 #pragma warning( push )
00024 #pragma warning( disable : 4996 ) // vsprintf declared deprecated
00025
00026
00027 namespace TestToolBox
00028 {
00029
00030
00031 enum EventContext
00032 {
00033 CTX_NO_CONTEXT = 0x8000,
00034
00035 CTX_TIMEOUT_ELAPSED = 0x0001,
00036 CTX_TOO_MANY_EVENTS = 0x0002,
00037 CTX_UNEXPECTED_EVENTS = 0x0004,
00038
00039 CTX_PROT = 0x0008,
00040 CTX_INFO = 0x0010,
00041 CTX_WARNING = 0x0020,
00042 CTX_ERROR = 0x0040,
00043 CTX_FATAL_ERROR = 0x0080,
00044 CTX_TEST_EVENT = 0x0100,
00045
00046 CTX_ALL = 0xfff8,
00047 CTX_ONLY_ERROR = CTX_ERROR | CTX_FATAL_ERROR
00048 };
00049
00050
00051
00052
00053
00054
00055
00056
00057 struct IEventReceiver
00058 {
00059
00060 IEventReceiver()
00061 : m_splitAtNewLine(false)
00062 {}
00063
00064
00065 virtual void EventMsg(
00066 EventContext in_context,
00067 const char* in_eventDescription) = 0;
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 virtual void Event(
00078 EventContext in_context,
00079 const char* in_eventDescription, ...)
00080 {
00081 UTIL_GET_FORMATTED_LINE_NO_NEWLINE (in_eventDescription);
00082 SplitLineAndAddPrefixIfNeeded(in_context, formattedLine);
00083 };
00084
00085
00086 virtual void Event(
00087 const char* in_eventDescription, ...)
00088 {
00089 UTIL_GET_FORMATTED_LINE_NO_NEWLINE (in_eventDescription);
00090 SplitLineAndAddPrefixIfNeeded(CTX_NO_CONTEXT, formattedLine);
00091 };
00092
00093
00094 virtual void Event(
00095 EventContext in_context,
00096 std::string const in_eventDescription)
00097 {
00098 SplitLineAndAddPrefixIfNeeded(in_context, in_eventDescription);
00099 }
00100
00101
00102 virtual void Event(
00103 std::string const in_eventDescription)
00104 {
00105 SplitLineAndAddPrefixIfNeeded(CTX_NO_CONTEXT, in_eventDescription);
00106 }
00107
00108
00109
00110
00111
00112
00113 void SetEventPrefix (std::string const & in_eventPrefix)
00114 {m_eventPrefix = in_eventPrefix;}
00115
00116
00117
00118 void SetSplitAtNewLine (bool in_split)
00119 {m_splitAtNewLine = in_split;}
00120
00121
00122
00123
00124
00125
00126 protected:
00127 std::string m_eventPrefix;
00128 bool m_splitAtNewLine;
00129
00130 std::string AddPrefix (
00131 std::string const & in_eventDescription)
00132 {
00133 if (m_eventPrefix.empty()) return in_eventDescription;
00134
00135 std::string eventStr = m_eventPrefix + "(\"" + in_eventDescription + "\");";
00136 return eventStr;
00137 }
00138
00139 void SplitLineAndAddPrefixIfNeeded (
00140 EventContext in_context,
00141 std::string const & in_eventDescription)
00142 {
00143 if (m_splitAtNewLine)
00144 {
00145 StringPosition curPos;
00146 EventContext context = in_context;
00147 while (GetNextLineStr(in_eventDescription, curPos))
00148 {
00149 EventMsg(context, AddPrefix(curPos.m_lineStr).c_str());
00150
00151 context = CTX_INFO;
00152 }
00153 }
00154 else
00155 {
00156 EventMsg(in_context, AddPrefix(in_eventDescription).c_str());
00157 }
00158
00159 }
00160 struct StringPosition
00161 {
00162 explicit StringPosition (
00163 std::string::size_type in_pos = 0)
00164 : m_curPos (in_pos)
00165 , m_lastPos (in_pos)
00166 , m_lineStr ()
00167 {}
00168
00169 std::string::size_type m_curPos;
00170 std::string::size_type m_lastPos;
00171 std::string m_lineStr;
00172 };
00173
00174 static bool GetNextLineStr (
00175 std::string const & in_stringWithNewLines,
00176 StringPosition & io_current)
00177 {
00178 if (io_current.m_lastPos == std::string::npos) return false;
00179
00180 io_current.m_curPos = in_stringWithNewLines.find_first_of( '\n', io_current.m_lastPos);
00181 io_current.m_lineStr = in_stringWithNewLines.substr(io_current.m_lastPos,
00182 io_current.m_curPos - io_current.m_lastPos);
00183
00184 if (io_current.m_curPos != std::string::npos)
00185 io_current.m_lastPos = io_current.m_curPos + 1;
00186 else
00187 io_current.m_lastPos = std::string::npos;
00188
00189 return true;
00190 }
00191
00192
00193
00194
00195 };
00196
00197 };
00198
00199 #pragma warning( pop )
00200
00201