You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
3.7KB

  1. #include "rack.hpp"
  2. #include "CLog.h"
  3. char Days[7][4]=
  4. {
  5. {"Sun"},
  6. {"Mon"},
  7. {"Tue"},
  8. {"Wed"},
  9. {"Thu"},
  10. {"Fri"},
  11. {"Sat"}
  12. };
  13. char Months[12][4]=
  14. {
  15. {"Jan"},
  16. {"Feb"},
  17. {"Mar"},
  18. {"Apr"},
  19. {"May"},
  20. {"Jun"},
  21. {"Jul"},
  22. {"Aug"},
  23. {"Sep"},
  24. {"Oct"},
  25. {"Nov"},
  26. {"Dec"}
  27. };
  28. //-----------------------------------------------------
  29. // Function:
  30. //
  31. //-----------------------------------------------------
  32. CLog::CLog()
  33. {
  34. m_LogLevel = 1;
  35. fp = NULL;
  36. m_LogCallbackFunc = NULL;
  37. }
  38. //-----------------------------------------------------
  39. // Function:
  40. //
  41. //-----------------------------------------------------
  42. CLog::~CLog()
  43. {
  44. Close();
  45. }
  46. //-----------------------------------------------------
  47. // Function:
  48. //
  49. //-----------------------------------------------------
  50. int CLog::Open( std::string strFileName )
  51. {
  52. // test if this file is valid
  53. if ( (fp = fopen( strFileName.c_str(), "w+" ) ) == NULL )
  54. return 0;
  55. // return success
  56. return 1;
  57. }
  58. //-----------------------------------------------------
  59. // Function:
  60. //
  61. //-----------------------------------------------------
  62. void CLog::Close(void)
  63. {
  64. // if file not open then bail
  65. if (fp)
  66. {
  67. // close the file handle
  68. fclose(fp);
  69. fp = NULL;
  70. }
  71. }
  72. //-----------------------------------------------------
  73. // Function: SetLogLvl
  74. //
  75. //-----------------------------------------------------
  76. void CLog::SetLogLvl( int level )
  77. {
  78. m_LogLevel = level;
  79. }
  80. //-----------------------------------------------------
  81. // Function: SetCallback
  82. //
  83. //-----------------------------------------------------
  84. void CLog::SetCallback ( LOGCALLBACK *func, void *pClass )
  85. {
  86. m_LogCallbackFunc = func;
  87. m_pCallbackClass = pClass;
  88. }
  89. void CLog::f( std::string string, ... )
  90. {
  91. // if file not open then bail
  92. if (!fp)
  93. return;
  94. //_Xtime_get_ticks(
  95. va_list arglist; // variable argument list
  96. // print out the string using the variable number of arguments on stack
  97. va_start( arglist, string );
  98. vsprintf( (char*)buffer.c_str(), string.c_str(), arglist );
  99. va_end( arglist );
  100. // write string to file
  101. fprintf( fp,(char*)buffer.c_str() );
  102. // write string to callback so user can display it somewhere
  103. if( m_LogCallbackFunc )
  104. m_LogCallbackFunc( m_pCallbackClass, (char*)buffer.c_str() );
  105. fflush(fp);
  106. }
  107. //-----------------------------------------------------
  108. // Function: no timestamp
  109. //
  110. //-----------------------------------------------------
  111. void CLog::fnr( std::string string, ...)
  112. {
  113. va_list arglist; // variable argument list
  114. // if file not open then bail
  115. if (!fp)
  116. return;
  117. // print out the string using the variable number of arguments on stack
  118. va_start( arglist,string );
  119. vsprintf( (char*)buffer.c_str(), string.c_str(),arglist );
  120. va_end( arglist );
  121. // write string to file
  122. fprintf( fp, (char*)buffer.c_str() );
  123. // write string to callback so user can display it somewhere
  124. if( m_LogCallbackFunc )
  125. m_LogCallbackFunc( m_pCallbackClass, (char*)buffer.c_str() );
  126. fflush(fp);
  127. }
  128. //-----------------------------------------------------
  129. // Function: mem - memory dump
  130. //
  131. //-----------------------------------------------------
  132. void CLog::mem( unsigned char *pBuff, unsigned int dwSize, unsigned int dwOff )
  133. {
  134. int i;
  135. unsigned int Offset=dwOff, index=0;
  136. unsigned char Val[16];
  137. // if file not open then bail
  138. if (!fp)
  139. return;
  140. do{
  141. f("0x%.4x: ", dwOff);
  142. Offset+=16;
  143. dwOff+=16;
  144. for(i=0; i<16; i++)
  145. {
  146. // extra space every four
  147. if(i%4 == 0)
  148. f(" ");
  149. if( index < dwSize )
  150. {
  151. Val[i]=pBuff[index];
  152. f("%.2x ", pBuff[index++]);
  153. }
  154. else
  155. {
  156. Val[i]=0;
  157. f("-- ", pBuff[index++]);
  158. }
  159. }
  160. // print characters
  161. for(i=0; i<16; i++)
  162. {
  163. if( isprint( Val[i] ) )
  164. f("%c", Val[i]);
  165. else
  166. f(".");
  167. }
  168. f("\n");
  169. }while(index < dwSize);
  170. f("\n\n");
  171. }