|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * System Includes |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 |
|
21 #include <stdio.h> |
|
22 #include <assert.h> |
|
23 |
|
24 |
|
25 /******************************************************************************* |
|
26 * |
|
27 * Local Includes |
|
28 * |
|
29 ******************************************************************************/ |
|
30 #include "CLog.h" |
|
31 |
|
32 |
|
33 /******************************************************************************* |
|
34 * |
|
35 * Definitions |
|
36 * |
|
37 ******************************************************************************/ |
|
38 #define MIN(a,b) (((a) < (b)) ? (a) : (b)) |
|
39 #define PRINT_ENTRY_PREFIX_LEN 16 |
|
40 #define PRINT_ENTRY_SEPARATOR (" - ") |
|
41 #define PRINT_ENTRY_SEPARATOR_LEN 3 |
|
42 #define MAXLINESIZE (PRINT_ENTRY_PREFIX_LEN + PRINT_ENTRY_SEPARATOR_LEN + MAXWHOSIZE + MAXMSGSIZE) |
|
43 #ifndef WIN32 |
|
44 #define _snprintf snprintf |
|
45 #endif |
|
46 |
|
47 /******************************************************************************* |
|
48 * |
|
49 * Statics |
|
50 * |
|
51 ******************************************************************************/ |
|
52 |
|
53 |
|
54 /******************************************************************************* |
|
55 * |
|
56 * Implementation |
|
57 * |
|
58 ******************************************************************************/ |
|
59 CLog::CLog( int aFileBacked ) |
|
60 { |
|
61 assert( aFileBacked == 0 ); |
|
62 iLogVector = 0xFFFFFFFF; |
|
63 } |
|
64 |
|
65 |
|
66 void CLog::WriteLogEntry( TSeverity aSeverity, char *aWho, char *aMsg ) |
|
67 { |
|
68 TLogEntry entry; |
|
69 int slen; |
|
70 |
|
71 // copy message into struct |
|
72 memset( &entry, 0, sizeof(entry) ); |
|
73 entry.iSeverity = aSeverity; |
|
74 slen = strlen(aWho); |
|
75 slen = MIN( slen, (MAXWHOSIZE-1) ); |
|
76 memcpy( entry.iWho, aWho, slen ); |
|
77 slen = strlen(aMsg); |
|
78 slen = MIN( slen, (MAXMSGSIZE-1) ); |
|
79 memcpy( entry.iMsg, aMsg, slen ); |
|
80 |
|
81 // now add it to the vector |
|
82 iLogEntries.push_back( entry ); |
|
83 } |
|
84 |
|
85 |
|
86 void CLog::WriteLogEntry( TSeverity aSeverity, char *aWho, char *aErrorLocation, int aErrorA, int aErrorB ) |
|
87 { |
|
88 char buff[MAXMSGSIZE]; |
|
89 _snprintf( buff, MAXMSGSIZE, "%s (%d, %d)", aErrorLocation, aErrorA, aErrorB ); |
|
90 WriteLogEntry( aSeverity, aWho, buff ); |
|
91 } |
|
92 |
|
93 |
|
94 void CLog::SetLogLevel( int aLogVector ) |
|
95 { |
|
96 iLogVector = aLogVector; |
|
97 } |
|
98 |
|
99 |
|
100 char *CLog::GetSeverityString( TSeverity aSeverity ) |
|
101 { |
|
102 switch( aSeverity ) { |
|
103 |
|
104 case SV_INFO: |
|
105 return "Info: "; |
|
106 |
|
107 case SV_WARNING: |
|
108 return "Warning: "; |
|
109 |
|
110 case SV_ERROR: |
|
111 return "Error: "; |
|
112 |
|
113 case SV_RESOURCE: |
|
114 return "Resource: "; |
|
115 |
|
116 case SV_STATE: |
|
117 return "State: "; |
|
118 |
|
119 default: |
|
120 return "Unknown: "; |
|
121 } |
|
122 |
|
123 // should never get here |
|
124 assert( !"INVALID CODE PATH" ); |
|
125 return NULL; |
|
126 } |
|
127 |
|
128 |
|
129 int CLog::CalculateLogSize() |
|
130 { |
|
131 char *severity_string; |
|
132 char cline[MAXLINESIZE]; |
|
133 int log_size = 0; |
|
134 |
|
135 vector<TLogEntry>::iterator iter; |
|
136 for( iter = iLogEntries.begin(); iter != iLogEntries.end(); iter++ ) { |
|
137 severity_string = GetSeverityString( iter->iSeverity ); |
|
138 sprintf( cline, "%s%s%s%s\n", severity_string, iter->iWho, PRINT_ENTRY_SEPARATOR, iter->iMsg ); |
|
139 log_size += strlen(cline); |
|
140 } |
|
141 log_size++; |
|
142 return log_size; |
|
143 } |
|
144 |
|
145 |
|
146 void CLog::PrintLogToBuffer( int aBufferSize, char *aBuffer ) |
|
147 { |
|
148 char cline[MAXLINESIZE]; |
|
149 vector<TLogEntry>::iterator iter; |
|
150 int slen, remaining_space; |
|
151 char *severity_string; |
|
152 |
|
153 // init params |
|
154 assert( aBufferSize > 0 ); |
|
155 assert( aBuffer != NULL ); |
|
156 |
|
157 // init |
|
158 aBuffer[0] = 0; |
|
159 remaining_space = aBufferSize; |
|
160 |
|
161 // process each entry |
|
162 for( iter = iLogEntries.begin(); iter != iLogEntries.end(); iter++ ) { |
|
163 |
|
164 // print the line to the temp buffer |
|
165 severity_string = GetSeverityString( iter->iSeverity ); |
|
166 sprintf( cline, "%s%s%s%s\n", severity_string, iter->iWho, PRINT_ENTRY_SEPARATOR, iter->iMsg ); |
|
167 |
|
168 // check there is room left in the passed buffer |
|
169 slen = strlen(cline); |
|
170 if( remaining_space <= slen ) { |
|
171 break; |
|
172 } |
|
173 |
|
174 // otherwise |
|
175 strcat( aBuffer, cline ); |
|
176 remaining_space -= slen; |
|
177 } |
|
178 |
|
179 // done |
|
180 return; |
|
181 } |
|
182 |