File: log.cpp

package info (click to toggle)
beid 3.5.2.dfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 147,240 kB
  • ctags: 34,507
  • sloc: cpp: 149,944; ansic: 41,577; java: 8,927; cs: 6,528; sh: 2,426; perl: 1,866; xml: 805; python: 463; makefile: 263; lex: 92
file content (249 lines) | stat: -rw-r--r-- 6,343 bytes parent folder | download
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* ****************************************************************************

 * eID Middleware Project.
 * Copyright (C) 2008-2009 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see
 * http://www.gnu.org/licenses/.

**************************************************************************** */
#ifdef WIN32
#include <windows.h>
#elif defined __APPLE__
#include "Mac/mac_helper.h"
#endif

#include <string.h>
#include <iostream>
#include <time.h>
#include <errno.h>

#include "diaglib.h"
#include "error.h"
#include "log.h"
#include "folder.h"

#include "svn_revision.h"
#include "beidversions.h"

#define LOGFILE L"diaglib.log"

static std::wstring g_wsLogFile;
static FILE *g_pfile=NULL;
static bool g_logFirstOpen=true;
static bool g_logAvailable=true;

////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////// PRIVATE FUNCTIONS DECLARATION ////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
int logLoadResources();
void getLocalTime(std::wstring &timestamp);
int logWrite(bool time, const wchar_t *format, va_list argList);

////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////// PUBLIC FUNCTIONS /////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
const char *diaglibVersion() 
{ 
	return BEID_PRODUCT_VERSION "." SVN_REVISION_STR; 
}

////////////////////////////////////////////////////////////////////////////////////////////////
int logGetFileName(const wchar_t **file)
{
	if(file==NULL)
		return DIAGLIB_ERR_BAD_CALL;

	if(g_wsLogFile.empty()) logInit(NULL);

	*file=g_wsLogFile.c_str();

	return DIAGLIB_OK;
}

////////////////////////////////////////////////////////////////////////////////////////////////
int logInit(const wchar_t *wzLogFile)
{
	int iReturnCode = DIAGLIB_OK;

	//Initialize g_wsLogFile
	if(wzLogFile==NULL)
	{
		if(DIAGLIB_OK != folderGetPath(FOLDER_TEMP,&g_wsLogFile,true))
		{
			return RETURN_LOG_INTERNAL_ERROR;
		}
		g_wsLogFile.append(LOGFILE);	
	}
	else if(wcslen(wzLogFile)==0)
	{
		return DIAGLIB_ERR_BAD_CALL;
	}
	else
	{
		g_wsLogFile.assign(wzLogFile);
	}
						
	return iReturnCode;
}

////////////////////////////////////////////////////////////////////////////////////////////////
int logLoadResources()
{
	int iReturnCode = DIAGLIB_OK;
		
	if(g_pfile)  return DIAGLIB_OK;
	
	if(g_wsLogFile.empty()) logInit(NULL);
	
	errno_t err = 0;
	
	//Create the file (Erase previous file)
	for(int i=0;i<LOG_OPEN_ATTEMPT_COUNT;i++)
	{
		err = _wfopen_s(&g_pfile, g_wsLogFile.c_str(), (g_logFirstOpen?L"w":L"a"));
		
		if (g_pfile)
		{
			break;
		}
		else if( err == ENOENT ) //Folder does not exist
		{
			g_logAvailable = false;
			return DIAGLIB_ERR_NOT_AVAILABLE;
		}
		else
		{
			Sleep(50);
		}
	}
	
	if(g_pfile && g_logFirstOpen)
	{
#ifdef WIN32
		fwprintf_s(g_pfile,L"RUNNING DIAGLIB VERSION: %hs\n",diaglibVersion());
#elif __APPLE__
		fwprintf_s(g_pfile,L"RUNNING DIAGLIB VERSION: %s\n",diaglibVersion());
#endif	
	}
	
	g_logFirstOpen = false;
	
	if (!g_pfile)
	{
		return DIAGLIB_ERR_FILE_CREATE_FAILED;
	}
	
	return iReturnCode;
}

////////////////////////////////////////////////////////////////////////////////////////////////
int logUnloadResources()
{
	if(g_pfile)
	{
		fclose(g_pfile);
		g_pfile=NULL;
	}
	return DIAGLIB_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////
int logFinalize(void)
{
	return logUnloadResources();
}


////////////////////////////////////////////////////////////////////////////////////////////////
int LOG(const wchar_t *format, ...)
{
	int iReturnCode = DIAGLIB_OK;
	
	if(!g_logAvailable)
		return DIAGLIB_OK;

	va_list args;
	va_start(args, format);
	iReturnCode = logWrite(false,format, args);
	va_end(args);

	return iReturnCode;
}

////////////////////////////////////////////////////////////////////////////////////////////////
int LOG_TIME(const wchar_t *format, ...)
{
	int iReturnCode = DIAGLIB_OK;
	
	if(!g_logAvailable)
		return DIAGLIB_OK;

	va_list args;
	va_start(args, format);
	iReturnCode = logWrite(true,format, args);
	va_end(args);

	return iReturnCode;
}
////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////// PRIVATE FUNCTIONS ////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
void getLocalTime(std::wstring &timestamp)
{
	time_t rawtime;
	struct tm timeinfo;
	wchar_t buffer [20];

	time ( &rawtime );
#ifdef WIN32
	localtime_s( &timeinfo, &rawtime );
#else
	timeinfo = *(localtime(&rawtime));
#endif

	wcsftime (buffer,20,L"%Y-%m-%d %H:%M:%S",&timeinfo);

	timestamp.assign(buffer);
}

////////////////////////////////////////////////////////////////////////////////////////////////
int logWrite(bool time, const wchar_t *format, va_list argList)
{
	int iReturnCode = DIAGLIB_OK;
	
	if(!g_logAvailable)
		return DIAGLIB_OK;
	
	if(format==NULL)
		return DIAGLIB_ERR_BAD_CALL;

	if(!g_pfile) 
	{
		if(DIAGLIB_OK != (iReturnCode = logLoadResources()))
		{
		   return iReturnCode;
		}
	}
		   
	if(!g_pfile) return DIAGLIB_ERR_INTERNAL;
	
	if(time)
	{
		std::wstring timestamp;
		getLocalTime(timestamp);
		fwprintf_s(g_pfile,L"%ls - ",timestamp.c_str());
	}
	vfwprintf_s(g_pfile, format, argList);
	
	return DIAGLIB_OK;
}