log.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. {
  2. Free Pascal port of the OpenPTC C++ library.
  3. Copyright (C) 2001-2003, 2006, 2007, 2009-2011 Nikolay Nikolov ([email protected])
  4. Original C++ version by Glenn Fiedler ([email protected])
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version
  9. with the following modification:
  10. As a special exception, the copyright holders of this library give you
  11. permission to link this library with independent modules to produce an
  12. executable, regardless of the license terms of these independent modules,and
  13. to copy and distribute the resulting executable under terms of your choice,
  14. provided that you also meet, for each linked independent module, the terms
  15. and conditions of the license of that module. An independent module is a
  16. module which is not derived from or based on this library. If you modify
  17. this library, you may extend this exception to your version of the library,
  18. but you are not obligated to do so. If you do not wish to do so, delete this
  19. exception statement from your version.
  20. This library is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. Lesser General Public License for more details.
  24. You should have received a copy of the GNU Lesser General Public
  25. License along with this library; if not, write to the Free Software
  26. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. }
  28. {$IFNDEF WinCE}
  29. const
  30. LOG_filename = 'ptcpas.log';
  31. {$ELSE WinCE}
  32. function LOG_filename: WideString;
  33. var
  34. RequiredBufferLength: DWord;
  35. ReturnedPathLength: DWord;
  36. TempPathBuf: PWideChar;
  37. dummy: Byte;
  38. begin
  39. RequiredBufferLength := GetTempPathW(0, @dummy);
  40. TempPathBuf := GetMem(RequiredBufferLength * SizeOf(WideChar));
  41. try
  42. ReturnedPathLength := GetTempPathW(RequiredBufferLength, TempPathBuf);
  43. if ReturnedPathLength > RequiredBufferLength then
  44. begin
  45. { The temp path length increased between 2 consecutive calls to GetTempPath?! }
  46. Result := '';
  47. exit;
  48. end;
  49. Result := TempPathBuf;
  50. Result := Result + 'ptcpas.log';
  51. finally
  52. FreeMem(TempPathBuf);
  53. end;
  54. end;
  55. {$ENDIF WinCE}
  56. var
  57. LOG_create: Boolean = True;
  58. LOG_enabled: Boolean =
  59. {$IFDEF DEBUG}
  60. True;
  61. {$ELSE DEBUG}
  62. False;
  63. {$ENDIF DEBUG}
  64. LOG_file: Text;
  65. procedure LOG_open;
  66. begin
  67. AssignFile(LOG_file, LOG_filename);
  68. if LOG_create then
  69. begin
  70. Rewrite(LOG_file);
  71. Writeln(LOG_file, '[log start]');
  72. LOG_create := False;
  73. end
  74. else
  75. Append(LOG_file);
  76. end;
  77. procedure LOG_close;
  78. begin
  79. CloseFile(LOG_file);
  80. end;
  81. procedure LOG(const message: String);
  82. begin
  83. if not LOG_enabled then
  84. exit;
  85. LOG_open;
  86. Writeln(LOG_file, message);
  87. LOG_close;
  88. end;
  89. procedure LOG(const message: string; data: Boolean);
  90. begin
  91. if not LOG_enabled then
  92. exit;
  93. LOG_open;
  94. Write(LOG_file, message, ' = ');
  95. if data then
  96. Writeln(LOG_file, 'true')
  97. else
  98. Writeln(LOG_file, 'false');
  99. LOG_close;
  100. end;
  101. procedure LOG(const message: string; data: Integer);
  102. begin
  103. if not LOG_enabled then
  104. exit;
  105. LOG_open;
  106. Writeln(LOG_file, message, ' = ', data);
  107. LOG_close;
  108. end;
  109. procedure LOG(const message: string; data: DWord);
  110. begin
  111. if not LOG_enabled then
  112. exit;
  113. LOG_open;
  114. Writeln(LOG_file, message, ' = ', data);
  115. LOG_close;
  116. end;
  117. procedure LOG(const message: string; data: Int64);
  118. begin
  119. if not LOG_enabled then
  120. exit;
  121. LOG_open;
  122. Writeln(LOG_file, message, ' = ', data);
  123. LOG_close;
  124. end;
  125. procedure LOG(const message: string; data: QWord);
  126. begin
  127. if not LOG_enabled then
  128. exit;
  129. LOG_open;
  130. Writeln(LOG_file, message, ' = ', data);
  131. LOG_close;
  132. end;
  133. procedure LOG(const message: string; data: Single);
  134. begin
  135. if not LOG_enabled then
  136. exit;
  137. LOG_open;
  138. Writeln(LOG_file, message, ' = ', data);
  139. LOG_close;
  140. end;
  141. procedure LOG(const message: string; data: Double);
  142. begin
  143. if not LOG_enabled then
  144. exit;
  145. LOG_open;
  146. Writeln(LOG_file, message, ' = ', data);
  147. LOG_close;
  148. end;
  149. procedure LOG(const message: string; const data: String);
  150. begin
  151. if not LOG_enabled then
  152. exit;
  153. LOG_open;
  154. Writeln(LOG_file, message, ' = ', data);
  155. LOG_close;
  156. end;
  157. procedure LOG(const message: string; data: IPTCFormat);
  158. begin
  159. if not LOG_enabled then
  160. exit;
  161. LOG_open;
  162. Write(LOG_file, message, ' = Format(');
  163. if data = nil then
  164. Write(LOG_file, 'NIL')
  165. else
  166. begin
  167. Write(LOG_file, data.bits:2);
  168. if data.direct then
  169. begin
  170. Write(LOG_file, ',$', HexStr(data.r, 8), ',$', HexStr(data.g, 8), ',$', HexStr(data.b, 8));
  171. if data.a <> 0 then
  172. Write(LOG_file, ',$', HexStr(data.a, 8));
  173. end;
  174. end;
  175. Writeln(LOG_file, ')');
  176. LOG_close;
  177. end;
  178. procedure LOG(const message: string; data: TPTCError);
  179. begin
  180. if not LOG_enabled then
  181. exit;
  182. LOG_open;
  183. Writeln(LOG_file, message, ': ', data.message);
  184. LOG_close;
  185. end;