<commit_before>    ///////////////////////////////////////////////////////////////////////////
//
//   MOOS - Mission Oriented Operating Suite
//
//   A suit of Applications and Libraries for Mobile Robotics Research
//   Copyright (C) 2001-2005 Massachusetts Institute of Technology and
//   Oxford University.
//
//   This software was written by <NAME> at MIT 2001-2002 and Oxford
//   University 2003-2005. email: <EMAIL>.
//
//   This file is part of a  MOOS Core Component.
//
//   This program is free software; you can redistribute it and/or
//   modify it under the terms of the GNU General Public License as
//   published by the Free Software Foundation; either version 2 of the
//   License, or (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//   General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
//   02111-1307, USA.
//
//////////////////////////    END_GPL    //////////////////////////////////
// MOOSLogger.cpp: implementation of the CMOOSLogger class.
//
//////////////////////////////////////////////////////////////////////
#ifdef _WIN32
#pragma warning(disable : 4786)
#endif


#include "MOOS/libMOOS/App/MOOSApp.h"
#include "MOOS/libMOOS/Utils/MOOSUtilityFunctions.h"
#include "MOOS/libMOOS/Utils/MOOSAssert.h"

#include <time.h>
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <cstring>


#ifndef _WIN32
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#endif


using namespace std;
#include "MOOSLogger.h"

//maximum of logged columns...
#define MAX_SYNC_COLUMNS 255
#define MOOS_LOGGER_DEFAULT_PERIOD  2.0
#define COLUMN_WIDTH 18
#define DEFAULT_MONITOR_TIME 10.0
#define MIN_SYNC_LOG_PERIOD 0.1
#define DYNAMIC_NAME_SPACE 64
#define DEFAULT_WILDCARD_TIME 1.0 //how often to call into the DB to get a list of all variables if wild card loggin is turned on
#define DEFAULT_DOUBLE_PRECISION  5 //how many DP to use when logging double time stamps



//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMOOSLogger::CMOOSLogger()
{	
    //be default we don't need to be too fast..
    SetAppFreq(5);

    //if no file name is given log files will begin with "MOOS"
    m_sStemFileName = "MOOS";

    //by default make an a-log
    m_bAsynchronousLog = true;

    //by default make an s-log
    m_bSynchronousLog = true;

    //and log every second
    m_dfSyncLogPeriod = 1.0;

    //start immediately
    m_dfLastSyncLogTime = 0;

    //we have no immediate need to check monitored variables
    m_dfLastMonitorTime = MOOSTime();

    //no s-log lines written yet
    m_nSyncLines = 0;

    //by default (if no mission file is specified) log to a local directory
    m_sPath = "./";

    //in a directory with the following stem
    m_sStemFileName = "MOOSLog";

    //and append a time ot the stem
    m_bAppendFileTimeStamp = true;
	
	//by default use local time for directory names
	m_bUseUTCLogNames = false;

	//by default we do not log aux src field
	m_bLogAuxSrc = false;

	//by default to not mark (with a @) variables being sent from external communities
	m_bMarkExternalCommunityMessages=  false;
    
	//by default do not indicate data tyep with a D: or S: suffix
	m_bMarkDataType = false;

    //lets always sort mail by time...
    SortMailByTime(true);

}

CMOOSLogger::~CMOOSLogger()
{
	ShutDown();
}

bool CMOOSLogger::ShutDown()
{
	m_Comms.Close();
	return CloseFiles();
}


bool CMOOSLogger::CloseFiles()
{
    if(m_AsyncLogFile.is_open())
    {
        m_AsyncLogFile.close();
    }

    if(m_SyncLogFile.is_open())
    {
        m_SyncLogFile.close();
    }

    if(m_SystemLogFile.is_open())
    {
        m_SystemLogFile.close();
    }
	
	//crucially make sure teh zipping thread has stopped

#ifdef ZLIB_FOUND
	
	m_AlogZipper.Stop();
	
	if(m_bUseExcludedLog)
	{
	   m_XlogZipper.Stop();
	}

#endif
    return true;

}


bool CMOOSLogger::OnConnectToServer()
{
    //ok so now lets register our interest in all these MOOS vars!
    RegisterMOOSVariables();

    //additional variables that are intersting to us..
    m_Comms.Register("LOGGER_RESTART",0.5);

    return true;
}

bool CMOOSLogger::OnNewMail(MOOSMSG_LIST &NewMail)
{
    //these three calls look through the incoming mail
    //and handle all appropriate logging
    DoAsyncLog(NewMail);<commit_msg>The module is now subscribed to SHUTDOWN<commit_after>    ///////////////////////////////////////////////////////////////////////////
//
//   MOOS - Mission Oriented Operating Suite
//
//   A suit of Applications and Libraries for Mobile Robotics Research
//   Copyright (C) 2001-2005 Massachusetts Institute of Technology and
//   Oxford University.
//
//   This software was written by <NAME> at MIT 2001-2002 and Oxford
//   University 2003-2005. email: <EMAIL>.
//
//   This file is part of a  MOOS Core Component.
//
//   This program is free software; you can redistribute it and/or
//   modify it under the terms of the GNU General Public License as
//   published by the Free Software Foundation; either version 2 of the
//   License, or (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//   General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
//   02111-1307, USA.
//
//////////////////////////    END_GPL    //////////////////////////////////
// MOOSLogger.cpp: implementation of the CMOOSLogger class.
//
//////////////////////////////////////////////////////////////////////
#ifdef _WIN32
#pragma warning(disable : 4786)
#endif


#include "MOOS/libMOOS/App/MOOSApp.h"
#include "MOOS/libMOOS/Utils/MOOSUtilityFunctions.h"
#include "MOOS/libMOOS/Utils/MOOSAssert.h"

#include <time.h>
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <cstring>


#ifndef _WIN32
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#endif


using namespace std;
#include "MOOSLogger.h"

//maximum of logged columns...
#define MAX_SYNC_COLUMNS 255
#define MOOS_LOGGER_DEFAULT_PERIOD  2.0
#define COLUMN_WIDTH 18
#define DEFAULT_MONITOR_TIME 10.0
#define MIN_SYNC_LOG_PERIOD 0.1
#define DYNAMIC_NAME_SPACE 64
#define DEFAULT_WILDCARD_TIME 1.0 //how often to call into the DB to get a list of all variables if wild card loggin is turned on
#define DEFAULT_DOUBLE_PRECISION  5 //how many DP to use when logging double time stamps



//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMOOSLogger::CMOOSLogger()
{	
    //be default we don't need to be too fast..
    SetAppFreq(5);

    //if no file name is given log files will begin with "MOOS"
    m_sStemFileName = "MOOS";

    //by default make an a-log
    m_bAsynchronousLog = true;

    //by default make an s-log
    m_bSynchronousLog = true;

    //and log every second
    m_dfSyncLogPeriod = 1.0;

    //start immediately
    m_dfLastSyncLogTime = 0;

    //we have no immediate need to check monitored variables
    m_dfLastMonitorTime = MOOSTime();

    //no s-log lines written yet
    m_nSyncLines = 0;

    //by default (if no mission file is specified) log to a local directory
    m_sPath = "./";

    //in a directory with the following stem
    m_sStemFileName = "MOOSLog";

    //and append a time ot the stem
    m_bAppendFileTimeStamp = true;
	
	//by default use local time for directory names
	m_bUseUTCLogNames = false;

	//by default we do not log aux src field
	m_bLogAuxSrc = false;

	//by default to not mark (with a @) variables being sent from external communities
	m_bMarkExternalCommunityMessages=  false;
    
	//by default do not indicate data tyep with a D: or S: suffix
	m_bMarkDataType = false;

    //lets always sort mail by time...
    SortMailByTime(true);

}

CMOOSLogger::~CMOOSLogger()
{
	ShutDown();
}

bool CMOOSLogger::ShutDown()
{
	m_Comms.Close();
	return CloseFiles();
}


bool CMOOSLogger::CloseFiles()
{
    if(m_AsyncLogFile.is_open())
    {
        m_AsyncLogFile.close();
    }

    if(m_SyncLogFile.is_open())
    {
        m_SyncLogFile.close();
    }

    if(m_SystemLogFile.is_open())
    {
        m_SystemLogFile.close();
    }
	
	//crucially make sure teh zipping thread has stopped

#ifdef ZLIB_FOUND
	
	m_AlogZipper.Stop();
	
	if(m_bUseExcludedLog)
	{
	   m_XlogZipper.Stop();
	}

#endif
    return true;

}


bool CMOOSLogger::OnConnectToServer()
{
    //ok so now lets register our interest in all these MOOS vars!
    RegisterMOOSVariables();

    //additional variables that are intersting to us..
    m_Comms.Register("LOGGER_RESTART",0.5);

	//! @moos_subscribe	SHUTDOWN
	AddMOOSVariable("SHUTDOWN","SHUTDOWN","SHUTDOWN",0);
    return true;
}

bool CMOOSLogger::OnNewMail(MOOSMSG_LIST &NewMail)
{
    //these three calls look through the incoming mail
    //and handle all appropriate logging
    DoAsyncLog(NewMail);
{'fraction_non_alphanumeric': 0.15843008322202815, 'fraction_numerical': 0.014692283982328162, 'mean_word_length': 2.4214411247803165, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6931631', 'n_tokens_mistral': 3108, 'n_tokens_neox': 2930, 'n_words': 1012}