-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog2file.h
177 lines (150 loc) · 4.44 KB
/
log2file.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//log2file.h
//use #define UseLogging to enable logging
#if DEBUG
#define UseLogging //to enable logging to file
#endif
//global
char logFileName[MAX_PATH];
TCHAR logFileNameW[MAX_PATH];
//bool FirstStart = FALSE;
void Add2Log(TCHAR);
void Add2Log(LPSTR);
void Add2Log(TCHAR *txt, BOOL bLogTime);
void Add2Log(char *txt, BOOL bLogTime);
void Add2Log (TCHAR *lpszFormat, ...);
void Add2LogWtime (TCHAR *lpszFormat, ...);
int writefile(LPTSTR);
int newfile(LPSTR);
int newfile(TCHAR);
int appendfile(LPSTR);
//=================================================================================
//implementation
//=================================================================================
//-----------------------------------------------------------------------
// Add2Log - Add string to the the file
//
void Add2Log (TCHAR *lpszFormat, ...) {
int nBuf;//, i;
TCHAR szBuffer[512];
va_list args;
va_start(args, lpszFormat);
nBuf = vswprintf(szBuffer, lpszFormat, args);
Add2Log(szBuffer, false);
va_end(args);
}
//-----------------------------------------------------------------------
// Add2Log - Add string to the the file
//
void Add2LogWtime (TCHAR *lpszFormat, ...) {
int nBuf;//, i;
TCHAR szBuffer[512];
va_list args;
va_start(args, lpszFormat);
nBuf = vswprintf(szBuffer, lpszFormat, args);
Add2Log(szBuffer, true);
va_end(args);
}
//-----------------------------------------------------------------------
// appendfile - set the global filename to append text
//
int appendfile(char *filename)
{
//store the filename to use in char and tchar
sprintf(logFileName, filename);
mbstowcs(logFileNameW, logFileName, sizeof(logFileName)*sizeof(logFileName[0]));
FILE *fp;
fp = fopen(logFileName, "a+");
fclose(fp);
return 0;
}
int newfile(char *filename)
{
//store the filename to use in char and tchar
sprintf(logFileName, filename);
mbstowcs(logFileNameW, logFileName, sizeof(logFileName)*sizeof(logFileName[0]));
FILE *fp;
fp = fopen(logFileName, "w+");
fclose(fp);
return 0;
}
int newfile(TCHAR *filename)
{
//store the filename to use in char and tchar
TCHAR logFileNameW[MAX_PATH];
wsprintf(logFileNameW, filename);
wcstombs(logFileName, logFileNameW, sizeof(logFileNameW)*sizeof(logFileNameW[0]));
FILE *fp;
fp = fopen(logFileName, "w+");
fclose(fp);
return 0;
}
int writefile(TCHAR *filetext)
{
#ifdef UseLogging
/* File Write Function, written by professor chemicalX */
FILE *fp; /* Declare FILE structure */
TCHAR szTemp[255];
char szTempA[255];
wsprintf(szTemp, L"%s", filetext);
wcstombs(szTempA, szTemp, sizeof(szTemp)/sizeof(TCHAR));
fp = fopen(logFileName, "a+");
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* First of we open the file supplied by the filename paremeter */
/*
* in the "a+" mode for appending, so if it doesnt exist its created. £
* fp = fopen(filename,"w"); // Open using the "w" mode for writing.
*/
long fsize = strlen(szTempA); /* Declare the long fsize with the length of the filetext */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* paremeter witch stores the text or data to right to the file. */
fwrite(szTempA, 1, fsize, fp); /* Write File */
fclose(fp); /* Remember to close the file stream after calling file functions, */
/* otherwise the file wont be created. */
#endif
return 0;
}
void Add2Log(TCHAR *txt, BOOL bLogTime = TRUE)
{
TCHAR str[512];
TCHAR lpTimeStr[128];
TCHAR lpDateStr[128];
LONG res;
wsprintf(str,L"");
//Read the system time
res = GetTimeFormat(LOCALE_SYSTEM_DEFAULT,
TIME_FORCE24HOURFORMAT,
NULL,
L"hh:mm:ss",
lpTimeStr,
sizeof (lpTimeStr ) * sizeof(TCHAR));
if (res == 0)
{
wcscpy(lpTimeStr, L"err");
}
//Read the system date
res = GetDateFormat( LOCALE_SYSTEM_DEFAULT,
NULL,
NULL,
L"dd.MM.yyyy",
lpDateStr,
sizeof (lpDateStr) * sizeof(TCHAR));
if (res == 0)
{
wcscpy(lpDateStr, L"err");
}
if (bLogTime == TRUE)
wsprintf(str, L"\t%s %s:\n%s", lpDateStr, lpTimeStr , txt);
else
wsprintf(str, L"%s", txt);
writefile(str);
}
void Add2Log(char *txt, BOOL bLogTime = TRUE)
{
TCHAR szTemp[512];
char szTempA[512];
wsprintf(szTemp, L"");
sprintf(szTempA, "");
sprintf(szTempA, "%s", txt);
mbstowcs(szTemp, szTempA, 512);//strlen(szTempA)*sizeof(szTempA[0]));
Add2Log(szTemp, bLogTime);
}