connectivity/com.nokia.tcf/native/TCFNative/Common/Source/ErrorMonitorData.cpp
changeset 60 9d2210c8eed2
equal deleted inserted replaced
59:c892c53c664e 60:9d2210c8eed2
       
     1 /*
       
     2 * Copyright (c) 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 the License "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 *
       
    16 */
       
    17 
       
    18 #include "stdafx.h"
       
    19 #include "ErrorMonitorData.h"
       
    20 #include "TCErrorConstants.h"
       
    21 #include <stdio.h>
       
    22 #include <sys/stat.h>
       
    23 
       
    24 #ifdef TCF_CLIENT
       
    25 #include "..\..\TCFClient\ClientManager.h"
       
    26 extern CClientManager* gManager;
       
    27 #endif
       
    28 
       
    29 #ifdef TCF_SERVER
       
    30 #include "..\..\TCFServer\ServerManager.h"
       
    31 extern CServerManager* gManager;
       
    32 #endif
       
    33 
       
    34 #ifdef _DEBUG
       
    35 extern BOOL gDoLogging;
       
    36 #endif
       
    37 
       
    38 //#define LOG_PERFORMANCE
       
    39 #ifdef LOG_PERFORMANCE
       
    40 # ifdef TCF_CLIENT
       
    41 static char* perfFileName="c:\\tcf\\clienterrorperf.txt";
       
    42 # else
       
    43 static char* perfFileName="c:\\tcf\\servererrorperf.txt";
       
    44 # endif
       
    45 static FILE *fpLog = NULL;
       
    46 static int numLogged=0;
       
    47 static void logPerf(char* msg);
       
    48 static void openPerf();
       
    49 static void closePerf();
       
    50 #define OPENPERF() openPerf()
       
    51 #define LOGPERF(s) logPerf(s)
       
    52 #define CLOSEPERF() closePerf()
       
    53 #else
       
    54 #define OPENPERF()
       
    55 #define LOGPERF(s)
       
    56 #define CLOSEPERF()
       
    57 #endif
       
    58 
       
    59 //#define LOG_ERRORMONITOR
       
    60 #if defined(LOG_ERRORMONITOR) && defined(_DEBUG)
       
    61 extern char TCDebugMsg[];
       
    62 #define TCDEBUGOPEN() if (gDoLogging) gManager->m_DebugLog->WaitForAccess();
       
    63 #define TCDEBUGLOGS(s) if (gDoLogging) sprintf(TCDebugMsg,"%s", s); if (gDoLogging) gManager->m_DebugLog->log(TCDebugMsg);
       
    64 #define TCDEBUGLOGA1(s, a1) if (gDoLogging) sprintf(TCDebugMsg, s, a1); if (gDoLogging) gManager->m_DebugLog->log(TCDebugMsg);
       
    65 #define TCDEBUGLOGA2(s, a1, a2) if (gDoLogging) sprintf(TCDebugMsg, s, a1, a2); if (gDoLogging) gManager->m_DebugLog->log(TCDebugMsg);
       
    66 #define TCDEBUGLOGA3(s, a1, a2, a3) if (gDoLogging) sprintf(TCDebugMsg, s, a1, a2, a3); if (gDoLogging) gManager->m_DebugLog->log(TCDebugMsg);
       
    67 #define TCDEBUGCLOSE() if (gDoLogging) gManager->m_DebugLog->ReleaseAccess();
       
    68 #else
       
    69 #define TCDEBUGOPEN()
       
    70 #define TCDEBUGLOGS(s)
       
    71 #define TCDEBUGLOGA1(s, a1)
       
    72 #define TCDEBUGLOGA2(s, a1, a2)
       
    73 #define TCDEBUGLOGA3(s, a1, a2, a3)
       
    74 #define TCDEBUGCLOSE()
       
    75 #endif
       
    76 
       
    77 CErrorMonitor::CErrorMonitor()
       
    78 {
       
    79 }
       
    80 CErrorMonitor::CErrorMonitor(long inClientID)
       
    81 {
       
    82 	TCDEBUGOPEN();
       
    83 	TCDEBUGLOGA2("CErrorMonitor::CErrorMonitor clientId = %d this = %x\n", inClientID, this);
       
    84 
       
    85 	m_ClientID = inClientID;
       
    86 
       
    87 	TCDEBUGLOGS("CErrorMonitor::CErrorMonitor done\n");
       
    88 	TCDEBUGCLOSE();
       
    89 
       
    90 	OPENPERF();
       
    91 }
       
    92 CErrorMonitor::~CErrorMonitor()
       
    93 {
       
    94 	TCDEBUGOPEN();
       
    95 	TCDEBUGLOGA2("CErrorMonitor::~CErrorMonitor clientId = %d this = %x\n", m_ClientID, this);
       
    96 
       
    97 	m_Data.Close();
       
    98 	m_Mutex.Close();
       
    99 
       
   100 	TCDEBUGLOGS("CErrorMonitor::~CErrorMonitor done\n");
       
   101 	TCDEBUGCLOSE();
       
   102 
       
   103 	CLOSEPERF();
       
   104 }
       
   105 
       
   106 BOOL CErrorMonitor::CreateData()
       
   107 {
       
   108 	TCDEBUGOPEN();
       
   109 	TCDEBUGLOGA2("CErrorMonitor::CreateData clientId = %d this = %x\n", m_ClientID, this);
       
   110 
       
   111 	// create shared data location
       
   112 	char toString[30];
       
   113 	sprintf(toString, "%s%04.4ld",ERRORMONITORDATA_MAP_BASENAME, m_ClientID);
       
   114 	m_Data.Open(ERRORMONITORDATA_MAP_SIZE, toString);
       
   115 	m_Data.Init();
       
   116 
       
   117 	sprintf(toString, "%s%04.4ld",ERRORMONITORDATA_MUTEX_BASENAME, m_ClientID);
       
   118 
       
   119 	m_Mutex.Open(toString, ERRORMONITORDATA_MUTEX_TIMEOUT);
       
   120 
       
   121 	TCDEBUGLOGS("CErrorMonitor::CreateData done\n");
       
   122 	TCDEBUGCLOSE();
       
   123 
       
   124 	return TRUE;
       
   125 }
       
   126 
       
   127 BOOL CErrorMonitor::GetError(LONG* tcfError, BOOL* osErrorUsed, DWORD* osError)
       
   128 {
       
   129 	TCDEBUGOPEN();
       
   130 //	TCDEBUGLOGA2("CErrorMonitor::GetError clientId = %d this = %x\n", m_ClientID, this);
       
   131 	BOOL found = FALSE;
       
   132 
       
   133 	WaitForAccess();
       
   134 
       
   135 	pErrorData pData = GetDataPtr();
       
   136 
       
   137 #if (0)
       
   138 	if (pData->errorOccurred)
       
   139 	{
       
   140 		TCDEBUGLOGA3("CErrorMonitor::GetError tcfError = %d osErrorUsed = %d osError = %d\n", 
       
   141 			pData->tcfError, pData->osErrorUsed, pData->osError);
       
   142 
       
   143 		found = TRUE;
       
   144 		*tcfError = pData->tcfError;
       
   145 		*osErrorUsed = pData->osErrorUsed;
       
   146 		*osError = pData->osError;
       
   147 
       
   148 		pData->errorOccurred = FALSE;
       
   149 	}
       
   150 #endif
       
   151 #ifdef LOG_PERFORMANCE
       
   152 	char msg[100];
       
   153 	sprintf(msg, "GetError numErrors=%d\n", pData->numberErrors);
       
   154 	LOGPERF(msg);
       
   155 #endif
       
   156 	if (!IsErrorQueueEmpty(pData))
       
   157 	{
       
   158 		LOGPERF("GetError\n");
       
   159 		found = TRUE;
       
   160 		*tcfError = pData->errors[pData->first].tcfError;
       
   161 		*osErrorUsed = pData->errors[pData->first].osErrorUsed;
       
   162 		*osError = pData->errors[pData->first].osError;
       
   163 		TCDEBUGLOGA3("CErrorMonitor::GetError tcfError = %d osErrorUsed = %d osError = %d\n", 
       
   164 			*tcfError, *osErrorUsed, *osError);
       
   165 		pData->numberErrors--;
       
   166 		if (pData->numberErrors <= 0)
       
   167 		{
       
   168 			ResetErrorQueue(pData);
       
   169 		}
       
   170 		else
       
   171 		{
       
   172 			pData->first++;
       
   173 			if (pData->first >= MAX_ERRORS)
       
   174 				pData->first = 0;
       
   175 		}
       
   176 	}
       
   177 #ifdef LOG_PERFORMANCE
       
   178 //	char msg[100];
       
   179 	sprintf(msg, "GetError numErrors=%d tcfError=%d\n", pData->numberErrors, *tcfError);
       
   180 	LOGPERF(msg);
       
   181 #endif
       
   182 	ReleaseAccess();
       
   183 
       
   184 //	TCDEBUGLOGS("CErrorMonitor::GetError done\n");
       
   185 	TCDEBUGCLOSE();
       
   186 	return found;
       
   187 }
       
   188 
       
   189 BOOL CErrorMonitor::PutError(LONG tcfError, BOOL osErrorUsed, DWORD osError)
       
   190 {
       
   191 	TCDEBUGOPEN();
       
   192 //	TCDEBUGLOGA2("CErrorMonitor::PutError clientId = %d this = %x\n", m_ClientID, this);
       
   193 
       
   194 	BOOL done = FALSE;
       
   195 
       
   196 	WaitForAccess();
       
   197 
       
   198 	pErrorData pData = GetDataPtr();
       
   199 	if (pData == NULL)
       
   200 		return done;
       
   201 #if (0)
       
   202 	if (!pData->errorOccurred)
       
   203 	{
       
   204 		done = TRUE;
       
   205 		pData->tcfError = tcfError;
       
   206 		pData->osErrorUsed = osErrorUsed;
       
   207 		pData->osError = osError;
       
   208 
       
   209 //		TCDEBUGLOGA3("CErrorMonitor::PutError tcfError = %d osErrorUsed = %d osError = %d\n", 
       
   210 //			pData->tcfError, pData->osErrorUsed, pData->osError);
       
   211 
       
   212 		pData->errorOccurred = TRUE;
       
   213 	}
       
   214 #endif
       
   215 #ifdef LOG_PERFORMANCE
       
   216 	char msg[100];
       
   217 	sprintf(msg, "PutError numErrors=%d tcfError=%d\n", pData->numberErrors, tcfError);
       
   218 	LOGPERF(msg);
       
   219 #endif
       
   220 	if (!IsErrorQueueFull(pData))
       
   221 	{
       
   222 		LOGPERF("PutError\n");
       
   223 		TCDEBUGLOGA3("CErrorMonitor::PutError tcfError = %d osErrorUsed = %d osError = %d\n", 
       
   224 			tcfError, osErrorUsed, osError);
       
   225 		done = TRUE;
       
   226 		pData->errors[pData->next].tcfError = tcfError;
       
   227 		pData->errors[pData->next].osErrorUsed = osErrorUsed;
       
   228 		pData->errors[pData->next].osError = osError;
       
   229 		pData->numberErrors++;
       
   230 		pData->next++;
       
   231 		if (pData->next >= MAX_ERRORS)
       
   232 			pData->next = 0;
       
   233 	}
       
   234 #ifdef LOG_PERFORMANCE
       
   235 //	char msg[100];
       
   236 	sprintf(msg, "PutError numErrors=%d\n", pData->numberErrors);
       
   237 	LOGPERF(msg);
       
   238 #endif
       
   239 	ReleaseAccess();
       
   240 
       
   241 //	TCDEBUGLOGS("CErrorMonitor::GetError done\n");
       
   242 	TCDEBUGCLOSE();
       
   243 	return done;
       
   244 }
       
   245 
       
   246 BOOL CErrorMonitorData::Init()
       
   247 {
       
   248 	if (IsCreator())
       
   249 	{
       
   250 		pErrorData pData = (pErrorData)GetDataPtr();
       
   251 //		pData->errorOccurred = FALSE;
       
   252 //		pData->osErrorUsed = FALSE;
       
   253 //		pData->osError = 0;
       
   254 //		pData->tcfError = TCAPI_ERR_NONE;
       
   255 		pData->first = pData->next = pData->numberErrors = 0;
       
   256 	}
       
   257 	return TRUE;
       
   258 }
       
   259 #ifdef LOG_PERFORMANCE
       
   260 static void logPerf(char* msg)
       
   261 {
       
   262 	if (fpLog)
       
   263 	{
       
   264 		SYSTEMTIME sTime;
       
   265 		GetLocalTime(&sTime);
       
   266 		fprintf(fpLog, 
       
   267 			"%02.2d%02.2d-%02.2d:%02.2d:%02.2d.%03.3d: %s",
       
   268 			sTime.wDay, sTime.wMonth, sTime.wHour, sTime.wMinute, sTime.wSecond, sTime.wMilliseconds,
       
   269 			msg);
       
   270 
       
   271 		numLogged++;
       
   272 		if ((numLogged % 1000) == 0)
       
   273 			fflush(fpLog);
       
   274 	}
       
   275 }
       
   276 static void openPerf()
       
   277 {
       
   278 	struct _stat buf;
       
   279 	char* dirname = "c:\\tcf";
       
   280 	int result = _stat(dirname, &buf);
       
   281 	if (result == 0) // exists
       
   282 	{
       
   283 		if (fpLog == NULL)
       
   284 			fpLog = _fsopen(perfFileName, "at", _SH_DENYNO);
       
   285 	}
       
   286 	else
       
   287 	{
       
   288 		fpLog = NULL;
       
   289 	}
       
   290 }
       
   291 static void closePerf()
       
   292 {
       
   293 	if (fpLog)
       
   294 	{
       
   295 		fflush(fpLog);
       
   296 		fclose(fpLog);
       
   297 	}
       
   298 	fpLog = NULL;
       
   299 }
       
   300 #endif