|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #if defined(XP_WIN) |
|
6 #include <windows.h> |
|
7 #endif |
|
8 |
|
9 |
|
10 #include <stdio.h> |
|
11 #include <string.h> |
|
12 #include <stdlib.h> |
|
13 #include <stdarg.h> |
|
14 |
|
15 #include "updatelogging.h" |
|
16 |
|
17 UpdateLog::UpdateLog() : logFP(nullptr) |
|
18 { |
|
19 } |
|
20 |
|
21 void UpdateLog::Init(NS_tchar* sourcePath, |
|
22 const NS_tchar* fileName, |
|
23 const NS_tchar* alternateFileName, |
|
24 bool append) |
|
25 { |
|
26 if (logFP) |
|
27 return; |
|
28 |
|
29 this->sourcePath = sourcePath; |
|
30 NS_tchar logFile[MAXPATHLEN]; |
|
31 NS_tsnprintf(logFile, sizeof(logFile)/sizeof(logFile[0]), |
|
32 NS_T("%s/%s"), sourcePath, fileName); |
|
33 |
|
34 if (alternateFileName && NS_taccess(logFile, F_OK)) { |
|
35 NS_tsnprintf(logFile, sizeof(logFile)/sizeof(logFile[0]), |
|
36 NS_T("%s/%s"), sourcePath, alternateFileName); |
|
37 } |
|
38 |
|
39 logFP = NS_tfopen(logFile, append ? NS_T("a") : NS_T("w")); |
|
40 } |
|
41 |
|
42 void UpdateLog::Finish() |
|
43 { |
|
44 if (!logFP) |
|
45 return; |
|
46 |
|
47 fclose(logFP); |
|
48 logFP = nullptr; |
|
49 } |
|
50 |
|
51 void UpdateLog::Flush() |
|
52 { |
|
53 if (!logFP) |
|
54 return; |
|
55 |
|
56 fflush(logFP); |
|
57 } |
|
58 |
|
59 void UpdateLog::Printf(const char *fmt, ... ) |
|
60 { |
|
61 if (!logFP) |
|
62 return; |
|
63 |
|
64 va_list ap; |
|
65 va_start(ap, fmt); |
|
66 vfprintf(logFP, fmt, ap); |
|
67 fprintf(logFP, "\n"); |
|
68 va_end(ap); |
|
69 } |
|
70 |
|
71 void UpdateLog::WarnPrintf(const char *fmt, ... ) |
|
72 { |
|
73 if (!logFP) |
|
74 return; |
|
75 |
|
76 va_list ap; |
|
77 va_start(ap, fmt); |
|
78 fprintf(logFP, "*** Warning: "); |
|
79 vfprintf(logFP, fmt, ap); |
|
80 fprintf(logFP, "***\n"); |
|
81 va_end(ap); |
|
82 } |