locationtriggering/ltlogger/inc/lbtlogger.h
changeset 0 667063e416a2
equal deleted inserted replaced
-1:000000000000 0:667063e416a2
       
     1 /*
       
     2 * Copyright (c) 2007 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:  Defines the macros for logging mechanism in LBT
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef LBT_LOGGER_H
       
    20 #define LBT_LOGGER_H
       
    21 
       
    22 /**
       
    23  * To use this logger mechanism you also need to add flogger.lib to the mmp
       
    24  * using,
       
    25  * DEBUGLIBRARY flogger.lib
       
    26  */
       
    27 
       
    28 
       
    29 /**
       
    30  * The trace level is defined as follows,
       
    31  * 0x[moduleid - 4digits][logginglevel-4digits],
       
    32  * The currently define logginglevels are
       
    33  * 0x0001 - Verbose
       
    34  * things like entered method, leaving method etc,
       
    35  * logs of the parameters to methods, files 
       
    36  * that are being used, creating a key, creating a file,
       
    37  * opening a resource, 
       
    38  *
       
    39  * 0x0002 - Warning
       
    40  * things like events, error cases that were not processed 
       
    41  * but ignored.
       
    42  *
       
    43  * 0x0003 - Operations 
       
    44  * things like what operation started (create, delete), 
       
    45  * manipulations to the data structures, important events like
       
    46  * MMC card , app uninstall events which we process,
       
    47  * current location, triggers being monitored, psy conked off,
       
    48  * any other such important event.
       
    49  *
       
    50  * 0x0004 - Errors
       
    51  * log all _ASSERT_DEBUG kind of cases, basically things which 
       
    52  * you don't expect to happen but happened - Sounds like paradox ;-)
       
    53  * Also log all error cases with this trace level.
       
    54  *
       
    55  * 0x0005 - Fatal
       
    56  * Use this if the server cannot recover from this error and you 
       
    57  * have to crash !! ;-)
       
    58  *
       
    59  * So for a create trigger operation in server the log should be like
       
    60  * LBT_TRACE(KLbtLogOperation|KLbtLogServerLogic,"Creating Trigger");
       
    61  */
       
    62 #define KLbtLogLevelMask    0x0000FFFF
       
    63  
       
    64 #define KLbtLogVerbose      0x00000001
       
    65 #define KLbtLogWarning      0x00000002
       
    66 #define KLbtLogOperation    0x00000003
       
    67 #define KLbtLogError        0x00000004
       
    68 #define KLbtLogFatal        0x00000005
       
    69 
       
    70 /**
       
    71  *
       
    72  * Then for the module id lets use
       
    73  * 0x0001 - Lbt Client Library
       
    74  * 0x0002 - Lbt Server Core
       
    75  * 0x0003 - Lbt Server Logic Base
       
    76  * 0x0004 - Lbt Server Logic ( ecom plugin )
       
    77  * 0x0005 - Lbt Container
       
    78  * 0x0006 - Lbt Strategy Plugin api
       
    79  * 0x0007 - Lbt Strategy Implementation ( Ecom plugin )
       
    80  * 0x0008 - Lbt Movement detector plugin api
       
    81  * 0x0009 - Lbt Cell id Movement Detector
       
    82  * Did I miss any component ?
       
    83  */
       
    84 #define KLbtLogComponentMask            0xFFFF0000
       
    85  
       
    86 #define KLbtLogClientLib                0x00010000
       
    87 #define KLbtLogServerCore               0x00020000
       
    88 #define KLbtLogServerLogicBase          0x00030000
       
    89 #define KLbtLogServerLogic              0x00040000
       
    90 #define KLbtLogContainer                0x00050000
       
    91 #define KLbtLogStrategyPluginApi        0x00060000
       
    92 #define KLbtLogStrategyEngine           0x00070000
       
    93 #define KLbtLogMovementDetectorApi      0x00080000
       
    94 #define KLbtLogCellIdMovementDetector   0x00090000
       
    95 
       
    96 #ifdef _DEBUG
       
    97 #define _LBT_DEBUG
       
    98 #endif // #ifdef _DEBUG
       
    99 
       
   100 // define _LBT_DEBUG to enable LBT logging
       
   101 #define _LBT_DEBUG
       
   102 
       
   103 #ifdef _LBT_DEBUG
       
   104 
       
   105 #include <flogger.h>
       
   106 #include <f32file.h>
       
   107 #include "lbtloggerimpl.h"
       
   108 
       
   109 _LIT8( KEndOfLineChars, "\r\n");
       
   110 
       
   111 // CLASS DECLARATION
       
   112 class TLogFunc
       
   113 	{
       
   114 	public:
       
   115 		TLogFunc(const char aFunctionName[])
       
   116 			{
       
   117 			const unsigned char* string = (unsigned char*)(aFunctionName);
       
   118 			iBuffer.Zero();
       
   119 			iBuffer.Copy(string);			
       
   120 			TBuf8<150> buf;
       
   121 			buf.Zero();
       
   122 			buf.Append(_L("=>"));
       
   123 			buf.Append(iBuffer);
       
   124 			CLbtLoggerImpl* loggerImpl = CLbtLoggerImpl::CreateLogger();
       
   125 			if( loggerImpl )
       
   126 			    {
       
   127 			    RFileLogger& logger = loggerImpl->GetFileLogger();
       
   128 			    logger.Write( buf );
       
   129 			    }
       
   130 			}
       
   131 
       
   132 		~TLogFunc()
       
   133 			{
       
   134 			RThread thread;
       
   135 			TInt exitReason = thread.ExitReason();
       
   136 			if(exitReason != KErrNone)
       
   137 				{
       
   138 				TExitCategoryName name = thread.ExitCategory();
       
   139 				TBuf8<400> buf;
       
   140 				buf.Zero();
       
   141 				buf.Append(_L("LBT PANICKED : "));
       
   142 				buf.Append(name);
       
   143 				
       
   144 				CLbtLoggerImpl* loggerImpl = CLbtLoggerImpl::CreateLogger();
       
   145 				if(loggerImpl)
       
   146 					{
       
   147 					RFileLogger& logger = loggerImpl->GetFileLogger();				
       
   148 					logger.Write( buf);
       
   149 					}
       
   150 				// The thread has panicked and hence dont print the exit placeholder
       
   151 				thread.Close();
       
   152 				return;
       
   153 				}
       
   154 			iBuffer.Append(_L("=>"));
       
   155 			CLbtLoggerImpl* loggerImpl = CLbtLoggerImpl::CreateLogger();
       
   156 			if(loggerImpl)
       
   157 				{
       
   158 				RFileLogger& logger = loggerImpl->GetFileLogger();				
       
   159 				logger.Write( iBuffer);
       
   160 				}
       
   161 			thread.Close();
       
   162 			}
       
   163 		
       
   164 		static void FileName( TDes& aFileNameBuffer )
       
   165 		    {
       
   166 		    TTime currTime;
       
   167 		    currTime.HomeTime();
       
   168 		    TDateTime dt = currTime.DateTime();
       
   169 		    
       
   170 		    _LIT16( filefmt, "%S_%d%d%d_%d%d%d.log" );
       
   171 		    
       
   172 		    TBuf<150> logFileName;
       
   173 		    logFileName.Append( _L("C:\\Logs\\Epos\\lbt_func") );
       
   174 		    aFileNameBuffer.AppendFormat( filefmt(), &logFileName, dt.Day(), 
       
   175 		                                dt.Month(), dt.Year(), dt.Hour(), dt.Minute(), dt.Second() );
       
   176 		    }
       
   177 
       
   178 		static void FormatTextToBuffer( TDes8& aTrg, const TDesC8& aText )
       
   179 		    {
       
   180 		    TTime currTime;
       
   181 		    currTime.HomeTime();
       
   182 		    TDateTime dt = currTime.DateTime();
       
   183 		    _LIT8( dtFmt, "%d/%d/%d %d:%d:%d ");
       
   184 		    aTrg.AppendFormat( dtFmt(), dt.Day(), dt.Month(), dt.Year(), dt.Hour(), dt.Minute(), dt.Second() );
       
   185 		    aTrg.Append( aText );
       
   186 		    aTrg.Append( KEndOfLineChars );
       
   187 		    }
       
   188 		
       
   189 		static void RenameFileIfRequired()
       
   190 			{
       
   191 			RFs fs;
       
   192 		    RFile file;
       
   193 		    fs.Connect();
       
   194 		    
       
   195 			TInt err = file.Open( fs, _L("C:\\Logs\\Epos\\lbt_func.log"), EFileWrite|EFileShareAny );
       
   196 	        if( KErrNone == err )
       
   197 	            {
       
   198 	            // Check Size of file
       
   199 	            TInt size;
       
   200 	            file.Size( size );
       
   201 	            
       
   202 	            if( size > 1024 )
       
   203 	                {
       
   204 	                file.Close();
       
   205 	                TBuf<200> fileName;
       
   206 	                FileName( fileName );
       
   207 	                //BaflUtils::RenameFile( fs, _L("C:\\Logs\\Epos\\lbt.log"), fileName );
       
   208 	                fs.Rename(_L("C:\\Logs\\Epos\\lbt_func.log"), fileName );
       
   209 	                }	            
       
   210 	            }
       
   211 	        file.Close();
       
   212 	        fs.Close();
       
   213 			}
       
   214 
       
   215 	private:
       
   216 		TBuf8<150> iBuffer;
       
   217 	};
       
   218 
       
   219 #define FUNC_ENTER(x) //TLogFunc lbtlog(x);
       
   220 
       
   221 #define STOPLOGGING CLbtLoggerImpl::Destroy();
       
   222 
       
   223 #define ERROR(x,y)	{ TBuf8<150> buffer; \
       
   224 					  const unsigned char* string = (unsigned char*)(x); \
       
   225 					  buffer.Zero(); \
       
   226 					  buffer.Copy(string); \
       
   227 					  buffer.Append(_L(": LBT ERROR")); \
       
   228 					  CLbtLoggerImpl* loggerImpl = CLbtLoggerImpl::CreateLogger(); \
       
   229 					  if( loggerImpl ) \
       
   230 					    { \
       
   231 					    RFileLogger& logger = loggerImpl->GetFileLogger(); \
       
   232 					    logger.WriteFormat( buffer, y); \
       
   233 					    } \
       
   234 					}
       
   235 
       
   236 #define LOG(x)	{ TBuf8<150> buffer; \
       
   237 				  const unsigned char* string = (unsigned char*)(x); \
       
   238 				  buffer.Zero(); \
       
   239 				  buffer.Copy(string); \
       
   240 				  CLbtLoggerImpl* loggerImpl = CLbtLoggerImpl::CreateLogger(); \
       
   241 				  if( loggerImpl ) \
       
   242 				    { \
       
   243 				    RFileLogger& logger = loggerImpl->GetFileLogger(); \
       
   244 				    logger.Write( buffer); \
       
   245 				    } \
       
   246 				}
       
   247 				  
       
   248 #define LOG1(x,y)	{ TBuf8<150> buffer; \
       
   249 					  const unsigned char* string = (unsigned char*)(x); \
       
   250 					  buffer.Zero(); \
       
   251 					  buffer.Copy(string); \
       
   252 					  CLbtLoggerImpl* loggerImpl = CLbtLoggerImpl::CreateLogger(); \
       
   253 					  if( loggerImpl ) \
       
   254 					    { \
       
   255 					    RFileLogger& logger = loggerImpl->GetFileLogger(); \
       
   256 					    logger.WriteFormat( buffer, y); \
       
   257 					    } \
       
   258 					}
       
   259 					  
       
   260 #define LOGSTRING(x, y)  {  \
       
   261 						 _LIT(KTmpStr, x); \
       
   262 						 CLbtLoggerImpl* loggerImpl = CLbtLoggerImpl::CreateLogger(); \
       
   263 						 if( loggerImpl ) \
       
   264 						    { \
       
   265 						    RFileLogger& logger = loggerImpl->GetFileLogger(); \
       
   266 						    logger.WriteFormat( TRefByValue<const TDesC>(KTmpStr()), y); \
       
   267 						    } \
       
   268 					  	 } \
       
   269 
       
   270 
       
   271 #define LBT_FILE_LOGGER 1 // Always log to file
       
   272 
       
   273 const char* const LbtLogLevelStr[] = 
       
   274     {
       
   275     "",
       
   276     "Verbose",
       
   277     "Warning",
       
   278     "Operation",
       
   279     "Error",
       
   280     "Fatal"
       
   281     };
       
   282 
       
   283 const char* const LbtLogComponentStr[] = 
       
   284     {
       
   285     "",
       
   286     "client lib",
       
   287     "server core",
       
   288     "server logic api",
       
   289     "server logic",
       
   290     "container",
       
   291     "strategy api",
       
   292     "strategy engine",
       
   293     "movement detector api",
       
   294     "cell id movement detector"
       
   295     };
       
   296 
       
   297 
       
   298 #define LENGTH(str,len) for(len = 0; str[len]; len++) {}
       
   299 
       
   300 
       
   301 NONSHARABLE_CLASS( TLbtDesOverflow ) : public TDes8Overflow
       
   302     {
       
   303     void Overflow(TDes8 &/*aDes*/)
       
   304         {
       
   305         // Dummy Implementation to prevent User panic 23
       
   306         }
       
   307     };
       
   308     
       
   309 #define LBT_TRACE(level, file, lineno, desc...) 
       
   310 
       
   311 /*
       
   312 LOCAL_C void LBT_TRACE(TInt level, const char* file, TInt lineno, const char* desc,...)
       
   313     {
       
   314     _LIT8(fmt, "!%s %s %s:%d ");
       
   315     TInt desclength, filelength, fmtlength;
       
   316     fmtlength = fmt().Length();
       
   317     LENGTH(desc,desclength);
       
   318     LENGTH(file,filelength);
       
   319 
       
   320     HBufC8* str = NULL;
       
   321     TRAPD(error, str = HBufC8::NewL(desclength+fmtlength+filelength+10+25+200));
       
   322     if ( error != KErrNone )
       
   323         {
       
   324         return;
       
   325         }
       
   326     
       
   327     TPtr8 ptr(str->Des());
       
   328     ptr.Zero();
       
   329 
       
   330     TLbtDesOverflow ovrflowHandler;
       
   331     ptr.AppendFormat(fmt(),
       
   332                      &ovrflowHandler,
       
   333                      LbtLogLevelStr[(level&KLbtLogLevelMask)],
       
   334                      LbtLogComponentStr[(level&KLbtLogComponentMask) >> 16],
       
   335                      file,
       
   336                      lineno);
       
   337 
       
   338     HBufC8* descbuf = HBufC8::NewL(desclength);
       
   339     if ( error == KErrNone )
       
   340         {
       
   341         descbuf->Des().Append(reinterpret_cast<const TUint8 *>(desc), desclength);
       
   342         VA_LIST list;
       
   343         VA_START(list, desc);
       
   344         ptr.AppendFormat(*descbuf,
       
   345                          &ovrflowHandler,
       
   346                          list );
       
   347         delete descbuf;
       
   348         }
       
   349 
       
   350 #ifdef LBT_FILE_LOGGER
       
   351     _LIT(KLbtTraceDir,"epos");
       
   352     _LIT(KLbtTraceFile,"lbt.log");
       
   353     RFileLogger::Write(KLbtTraceDir(), KLbtTraceFile(), EFileLoggingModeAppend, *str);
       
   354 #else
       
   355     RDebug::RawPrint(*str);
       
   356 #endif
       
   357     delete str;
       
   358     }
       
   359 */
       
   360 
       
   361 #else // if _DEBUG flag is not defined
       
   362 
       
   363 #define LBT_TRACE(level, file, lineno, desc...) 
       
   364 #define FUNC_ENTER(x)
       
   365 #define STOPLOGGING
       
   366 #define LOG(x)
       
   367 #define LOG1(x,y)
       
   368 #define ERROR(x,y)
       
   369 #define LOGSTRING(x, y)
       
   370 
       
   371 #endif // end of #ifdef _DEBUG 
       
   372 
       
   373 
       
   374 #endif // LBT_LOGGER_H