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