/*
-----------------------------------------------------------------------------
This source file is part of OpenSpace3D
For the latest info, see http://www.openspace3d.com
Copyright (c) 2012 I-maginer
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser 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, or go to
http://www.gnu.org/copyleft/lesser.txt
-----------------------------------------------------------------------------
*/
/*
Version : 1.0
First version : 11/26/2010
Author : Aymeric Suteau
*/
var sStatsFilePath = "tmp/logs/";;
var elapsedTime = 0.0;;
/*-------------------- Global structure --------------------*/
struct DataStr = [
PDATA_name : S,
PDATA_value : S
] MkDataStr;;
struct StatsStr = [
PSTATS_inst : PInstance,
PSTATS_period : I,
PSTATS_timer : Timer,
PSTATS_bAddTime : I,
PSTATS_bElapsedTime : I,
PSTATS_file : W,
PSTATS_sFileName : S,
PSTATS_lParams : [DataStr r1],
PSTATS_bNewlog : I
] MkStatsStr;;
/*! \brief Callback on instance destruction
*
* Prototype: fun [PInstance] I
*
* \param PInstance : destroyed plugIT instance
*
* \return I : 0
**/
fun deleteOb(inst, statstr)=
if (statstr.PSTATS_timer == nil) then nil else
(
_deltimer statstr.PSTATS_timer;
set statstr.PSTATS_timer = nil;
set elapsedTime = 0.0;
);
0;;
/*! \brief Add a line to the log file
*
* Prototype: fun [PInstance] I
*
* \param PInstance : Log file Instance
*
* \return I : 0
**/
fun cbTimerWriteLog(trm, statstr)=
let sizelist statstr.PSTATS_lParams -> size in
let 0 -> i in
let "" -> logline in
(
while (i < size) do
(
let nth_list statstr.PSTATS_lParams i -> datastr in
// (ctoa 9) for tab character
set logline = strcatn logline::datastr.PDATA_value::";"::nil;
set i = i + 1;
);
// Check if system time must be written to file
if (!statstr.PSTATS_bAddTime) then nil else
let nth_list (hd strextr (ctime time)) 3 -> stime in
set logline = strcat (strcat stime ";") logline;
// Check if elapsed time must be written to file
if (!statstr.PSTATS_bElapsedTime) then nil else
(
set elapsedTime = elapsedTime +. (1.0 /. (1000.0 /. itof (statstr.PSTATS_period)));
set logline = strcat (strcat ftoa (elapsedTime -. (itof statstr.PSTATS_period) /. 1000.0) ";") logline;
);
if (!statstr.PSTATS_bNewlog) then nil else
SendPluginEvent statstr.PSTATS_inst "New log" logline nil;
_appendpack (strcat logline "\n") statstr.PSTATS_file;
);
0;;
/*! \brief Start log
*
* Start writing data to log file
*
* Prototype: fun [PInstance DMI S S I [F F F I]] I
*
* \param ObjLogfile : Log file Instance
* \param PInstance : plugIT instance
*
* \return I : 0
**/
fun cbStartLog(inst, from, action, param, reply, statstr)=
if (statstr.PSTATS_timer != nil) then nil else
set statstr.PSTATS_timer = _rfltimer _starttimer _channel statstr.PSTATS_period @cbTimerWriteLog statstr;
0;;
/*! \brief Stop log
*
* Stop writing data to log file
*
* Prototype: fun [PInstance DMI S S I [F F F I]] I
*
* \param ObjLogfile : Log file Instance
* \param PInstance : plugIT instance
*
* \return I : 0
**/
fun cbStopLog(inst, from, action, param, reply, statstr)=
if (statstr.PSTATS_timer == nil) then nil else
(
_deltimer statstr.PSTATS_timer;
set statstr.PSTATS_timer = nil;
);
0;;
/*! \brief Pause log
*
* Pause writing data to log file
*
* Prototype: fun [PInstance DMI S S I [F F F I]] I
*
* \param ObjLogfile : Log file Instance
* \param PInstance : plugIT instance
*
* \return I : 0
**/
fun cbPauseLog(inst, from, action, param, reply, statstr)=
if (statstr.PSTATS_timer == nil) then nil else
(
_deltimer statstr.PSTATS_timer;
set statstr.PSTATS_timer = nil;
);
0;;
/*! \brief Resume log
*
* Resume writing data to log file
*
* Prototype: fun [PInstance DMI S S I [F F F I]] I
*
* \param ObjLogfile : Log file Instance
* \param PInstance : plugIT instance
*
* \return I : 0
**/
fun cbResumeLog(inst, from, action, param, reply, statstr)=
if (statstr.PSTATS_timer != nil) then nil else
set statstr.PSTATS_timer = _rfltimer _starttimer _channel statstr.PSTATS_period @cbTimerWriteLog statstr;
0;;
/*! \brief change value
*
*
*
* Prototype: fun [PInstance DMI S S I [F F F I]] I
*
* \param ObjLogfile : Log file Instance
* \param PInstance : plugIT instance
*
* \return I : 0
**/
fun cbChangeValue(inst, from, action, param, reply, datastr)=
//remove possible \n at the end of param
let hd lineextr param -> param in
set datastr.PDATA_value = strtrim param;
0;;
/*! \brief Callback on new plugIT instance
*
* Create a new log file
*
* Prototype: fun [PInstance] I
*
* \param PInstance : plugIT instance
*
* \return I : 0
**/
fun newOb(inst)=
// Get log interval value from plugIT window
let atoi (getPluginInstanceParam inst "period") -> period in
let atoi (getPluginInstanceParam inst "addtime") -> addtime in
let if addtime == nil then 0 else addtime -> addtime in
let atoi (getPluginInstanceParam inst "addelapsedtime") -> addelapsedtime in
let if addelapsedtime == nil then 0 else addelapsedtime -> addelapsedtime in
let atoi (getPluginInstanceParam inst "autostart") -> autostart in
let getPluginInstanceUserActions inst -> laction in
let strcatn sStatsFilePath::(strreplace (strcatnSep (hd strextr (ctime time)) "_") ":" "-")::"_"::(getShortName getPluginInstanceFullname inst)::".csv"::nil -> filename in
let (IsInEditor inst) || IsEventLinked inst "New log" -> bnewlog in
let MkStatsStr [inst period nil addtime addelapsedtime nil filename nil bnewlog] -> statstr in
(
_deletepack _checkpack filename;
set statstr.PSTATS_file = _getmodifypack filename;
// Associate callbacks to actions
PluginRegisterAction inst "Start log" mkfun6 @cbStartLog statstr;
PluginRegisterAction inst "Stop log" mkfun6 @cbStopLog statstr;
PluginRegisterAction inst "Pause log" mkfun6 @cbPauseLog statstr;
PluginRegisterAction inst "Resume log" mkfun6 @cbResumeLog statstr;
let sizelist laction -> size in
let 0 -> i in
let nil -> lparams in
let "" -> logline in
(
while (i < size) do
(
let nth_list laction i -> actname in
let MkDataStr [actname ""] -> datastr in
(
set lparams = lcat lparams datastr::nil;
set logline = strcatn logline::actname::";"::nil;
PluginRegisterAction inst actname mkfun6 @cbChangeValue datastr;
);
set i = i + 1;
);
set statstr.PSTATS_lParams = lparams;
if (!addtime) then nil else
set logline = strcat "Time;" logline;
if (!addelapsedtime) then nil else
set logline = strcat "Elapsed time;" logline;
_appendpack (strcat logline "\n") statstr.PSTATS_file;
// Check if autostart has been selected
if !autostart then nil else
set statstr.PSTATS_timer = _rfltimer _starttimer _channel statstr.PSTATS_period @cbTimerWriteLog statstr;
);
// Define the callback to call when the plugin instance is deleted
setPluginInstanceCbDel inst mkfun2 @deleteOb statstr;
);
0;;
/*! \brief Global plugIT function to initialize the plugIT callbacks
*
* Prototype: fun [S] I
*
* \param S : plugIT file path
*
* \return I : 0
**/
fun IniPlug(file)=
PlugRegister @newOb nil;
setPluginEditor @dynamicedit;
0;;