tagging release
[dasher.git] / Src / DasherCore / BasicLog.cpp
blobcd7398a624f5e6f863f3ac753a518a820e54c9ed
1 #include "BasicLog.h"
3 #include <cmath>
4 #include <fstream>
5 #include <iostream>
7 #ifdef _WIN32
8 #include <sys/timeb.h>
9 #else
10 #include <sys/time.h>
11 #endif
13 CBasicLog::CBasicLog(Dasher::CEventHandler *pEventHandler, CSettingsStore *pSettingsStore) : CUserLogBase(pEventHandler, pSettingsStore) {
14 m_iSymbolCount = 0;
15 m_bStarted = false;
18 CBasicLog::~CBasicLog() {
19 EndTrial();
22 void CBasicLog::StartWriting() {
23 if(!m_bStarted) {
24 StartTrial();
25 m_bStarted = true;
29 void CBasicLog::StopWriting(float dNats) {
30 m_dBits += dNats / log(2.0);
33 void CBasicLog::AddSymbols(Dasher::VECTOR_SYMBOL_PROB* pVectorNewSymbolProbs, eUserLogEventType iEvent) {
34 m_iSymbolCount += pVectorNewSymbolProbs->size();
37 void CBasicLog::DeleteSymbols(int iNumToDelete, eUserLogEventType iEvent) {
38 m_iSymbolCount -= iNumToDelete;
41 void CBasicLog::NewTrial() {
42 EndTrial();
45 void CBasicLog::KeyDown(int iId, int iType, int iEffect) {
46 ++m_iKeyCount;
49 void CBasicLog::StartTrial() {
50 m_iSymbolCount = 0;
51 m_iKeyCount = 0;
52 m_dBits = 0.0;
53 m_strStartDate = GetDateStamp();
54 m_iInitialRate = GetLongParameter(LP_MAX_BITRATE);
57 void CBasicLog::EndTrial() {
58 if(!m_bStarted)
59 return;
61 std::string strFileName(GetStringParameter(SP_USER_LOC));
62 strFileName.append("dasher_basic.log");
64 std::ofstream oFile;
65 oFile.open(strFileName.c_str(), ios::out | ios::app);
67 oFile << "\"" << m_strStartDate << "\":\"" << GetDateStamp() << "\":" << m_iSymbolCount << ":" << m_dBits << ":" << m_iKeyCount << ":" << m_iInitialRate / 100.0 << ":" << GetLongParameter(LP_MAX_BITRATE) / 100.0 << ":\"" << GetStringParameter(SP_INPUT_FILTER) << "\":\"" << GetStringParameter(SP_ALPHABET_ID) << "\"" << std::endl;
69 oFile.close();
71 m_bStarted = false;
74 std::string CBasicLog::GetDateStamp() {
75 std::string strDateStamp = "";
77 #ifdef _WIN32
78 struct timeb sTimeBuffer;
79 #else
80 struct timeval sTimeBuffer;
81 struct timezone sTimezoneBuffer;
82 #endif
83 char* szTimeLine = NULL;
85 #ifdef _WIN32
86 ftime(&sTimeBuffer);
87 szTimeLine = ctime(&(sTimeBuffer.time));
88 #else
89 gettimeofday(&sTimeBuffer, &sTimezoneBuffer);
90 szTimeLine = ctime(&(sTimeBuffer.tv_sec));
91 #endif
93 return std::string(szTimeLine).substr(0, 24);