/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/

#include "SctsLog.h"

/*****************************************************************
Generic logging functions
******************************************************************/

SctsMutex SctsLog::logsLock;
std::map <int,SctsLog *> SctsLog::logs;

SctsLog *SctsLog::GetLog()
{
	std::map <int,SctsLog *>::iterator it;
	logsLock.Lock();
	if ((it=logs.find(SctsThread::GetID()))==logs.end()) 
	{
		logsLock.Unlock();
		return 0;
	}
	logsLock.Unlock();
	return it->second;
}

bool SctsLog::Clear()
{
	SctsLog *log=GetLog();
	LogEntry *_firstEntry;
	if (!log) return false;
	while (log->firstEntry)
	{
		_firstEntry=log->firstEntry;
		log->firstEntry=log->firstEntry->next;
		sctsFree(_firstEntry->data);
		sctsFree(_firstEntry);
	}
	return true;
}

bool SctsLog::push(void *data,long dataLen)
{
	LogEntry *newLogEntry;
	if (!data || !dataLen) return false;
	newLogEntry=(LogEntry *)sctsMalloc(sizeof(LogEntry));
	newLogEntry->next=firstEntry;
	newLogEntry->dataLen=0;
	newLogEntry->data=sctsMalloc(dataLen);
	sctsMemcpy(newLogEntry->data,data,dataLen);
	firstEntry=newLogEntry;
	return true;
}

bool SctsLog::Push(void *data,long dataLen)
{
	SctsLog *log=GetLog();
	if (!log) return false;
	return log->push(data,dataLen);
}

void *SctsLog::pop(long &dataLen)
{
	void *data;
	LogEntry *_firstEntry;
	if (!firstEntry) return 0;
	_firstEntry=firstEntry;
	firstEntry=_firstEntry->next;
	data=_firstEntry->data;
	dataLen=_firstEntry->dataLen;
	sctsFree(_firstEntry);
	return data;
}

void *SctsLog::Pop(long &dataLen)
{
	SctsLog *log=GetLog();
	if (!log) return 0;
	return log->pop(dataLen);
}

SctsLog::SctsLog()
{
	logsLock.Lock();
	logs[SctsThread::GetID()]=this;
	logsLock.Unlock();
	firstEntry=0;
}

SctsLog::~SctsLog()
{
	LogEntry *_firstEntry;
	while (firstEntry)
	{
		_firstEntry=firstEntry->next;
		sctsFree(firstEntry->data);
		sctsFree(firstEntry);
		firstEntry=_firstEntry;
	}
	logsLock.Lock();
	logs.erase(logs.find(SctsThread::GetID()));
	logsLock.Unlock();
}
