os/ossrv/genericservices/taskscheduler/SCHSVR/SSCH_UTL.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 // User includes
    17 #include "SchLogger.h"
    18 
    19 #ifdef __SCHLOGGING__
    20 
    21 
    22 
    23 
    24 //
    25 // ------> CSheduleServerLog (source)
    26 //
    27 
    28 CSheduleServerLog::CSheduleServerLog()
    29 	{
    30 	}
    31 
    32 CSheduleServerLog::~CSheduleServerLog()
    33 	{
    34 	iFile.Close();
    35 	iFs.Close();
    36 	}
    37 
    38 void CSheduleServerLog::ConstructL(const TDesC& aLogFileName)
    39 	{
    40 	// Literal constants
    41 	_LIT(KScheduleServerLoggingDirectoryE, "E:\\Logs\\SchSvr\\");
    42 	_LIT(KScheduleServerLoggingDirectoryD, "D:\\Logs\\SchSvr\\");
    43 	_LIT(KScheduleServerLoggingDirectory, "_:\\Logs\\SchSvr\\");
    44 		
    45 	User::LeaveIfError(iFs.Connect());
    46 	TFileName logFile;
    47 
    48 	// Log to drive D if possible
    49 	TInt error = iFs.MkDirAll(KScheduleServerLoggingDirectoryE);
    50 	if	(error == KErrAlreadyExists || error == KErrNone)
    51 		{
    52 		logFile.Append(KScheduleServerLoggingDirectoryE);
    53 		}
    54 	else
    55 		{
    56 		error = iFs.MkDirAll(KScheduleServerLoggingDirectoryD);
    57 		if	(error == KErrAlreadyExists || error == KErrNone)
    58 			{
    59 			logFile.Append(KScheduleServerLoggingDirectoryD);
    60 			}
    61 		else
    62 			{
    63 			// system drive directory
    64 			TBuf<15> loggingDirOnSysDrive(KScheduleServerLoggingDirectory);
    65 			loggingDirOnSysDrive[0] = 'A' + static_cast<TInt>(RFs::GetSystemDrive());
    66 			
    67 			error = iFs.MkDirAll(loggingDirOnSysDrive);
    68 			if	(error != KErrAlreadyExists && error < KErrNone)
    69 				User::Leave(error);
    70 			logFile.Append(loggingDirOnSysDrive);
    71 			}
    72 		}
    73 
    74 	// Open log file
    75 	TParsePtrC parser(aLogFileName);
    76 	logFile.Append(parser.Name());
    77 
    78 #ifdef __WINS__
    79 	logFile.Append(_L(".WINS"));
    80 #elif __MARM__
    81 	logFile.Append(_L(".MARM"));
    82 #endif
    83 #ifdef _DEBUG
    84 	logFile.Append(_L(".UDEB"));
    85 #else
    86 	logFile.Append(_L(".UREL"));
    87 #endif
    88 	logFile.Append(parser.Ext());
    89 	logFile.Append(_L(".TXT"));
    90 
    91 	User::LeaveIfError(iFile.Replace(iFs, logFile, EFileStreamText | EFileShareAny));
    92 	SeekEnd();
    93 
    94 	NewLine();
    95 	NewLine();
    96 	_LIT(KNewLogEntry, "=== NEW LOG ===");
    97 	Write(KNewLogEntry);
    98 	NewLine();
    99 	}
   100 
   101 CSheduleServerLog* CSheduleServerLog::NewL(const TDesC& aLogFileName)
   102 	{
   103 	CSheduleServerLog* self = new(ELeave) CSheduleServerLog();
   104 	CleanupStack::PushL(self);
   105 	self->ConstructL(aLogFileName);
   106 	CleanupStack::Pop();
   107 	return self;
   108 	}
   109 
   110 //
   111 //
   112 //
   113 
   114 void CSheduleServerLog::Log(TRefByValue<const TDesC> aFmt,...)
   115 	{
   116 	VA_LIST list;
   117 	VA_START(list, aFmt);
   118 
   119 	TBuf<1024> buf;
   120 	buf.AppendFormatList(aFmt, list);
   121 	WriteWithTimeStamp(buf);
   122 	NewLine();
   123 	}
   124 
   125 void CSheduleServerLog::LogList(TRefByValue<const TDesC> aFmt, VA_LIST aList)
   126 	{
   127 	TBuf<1024> buf;
   128 	buf.AppendFormatList(aFmt, aList);
   129 	WriteWithTimeStamp(buf);
   130 	NewLine();
   131 	}
   132 
   133 void CSheduleServerLog::SeekEnd()
   134 	{
   135 	TInt pos;
   136 	iFile.Seek(ESeekEnd, pos);
   137 	}
   138 
   139 //
   140 //
   141 //
   142 
   143 void CSheduleServerLog::Write(const TDesC& aText)
   144 	{
   145 	HBufC8* buf = HBufC8::New(aText.Length());
   146 	if	(!buf)
   147 		return;
   148 
   149 	TPtr8 pBuf(buf->Des());
   150 	
   151 	const TInt KTextLength = aText.Length();
   152 	for(TInt i=0; i<KTextLength; i++)
   153 		{
   154 		if	(aText[i] < 256)
   155 			pBuf.Append(aText[i]);
   156 		}
   157 	
   158 	// Ignore errors
   159 	TInt error = iFile.Write(pBuf);
   160 	delete buf;
   161 	error = iFile.Flush();	
   162 	}
   163 
   164 void CSheduleServerLog::Write(const TDesC& aFmt, VA_LIST& aList)
   165 	{
   166 	TDes* buf = new TBuf<1000>;
   167 	if	(!buf)
   168 		return;
   169 	buf->AppendFormatList(aFmt, aList);
   170 	Write(*buf);
   171 	delete buf;
   172 	}
   173 
   174 void CSheduleServerLog::WriteWithTimeStamp(const TDesC& aText)
   175 	{
   176 	TBuf<200> buf;
   177 	TTime now;
   178 	now.HomeTime();
   179 	TDateTime dateTime;
   180 	dateTime = now.DateTime();
   181 	buf.Format(_L("%02d.%02d:%02d:%06d "), dateTime.Hour(), dateTime.Minute(), dateTime.Second(), dateTime.MicroSecond());
   182 	Write(buf);
   183 	Write(aText);
   184 	}
   185 
   186 void CSheduleServerLog::NewLine()
   187 	{
   188 	TBuf<2> buf;
   189 	buf.Append(0x0D);
   190 	buf.Append(0x0A);
   191 	Write(buf);
   192 	}
   193 
   194 
   195 #endif