/*
-----------------------------------------------------------------------------
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
-----------------------------------------------------------------------------
*/
var sLogFilePath = "tmp/logs/";;
/*-------------------- Global structure --------------------*/
struct DataStr = [
PDATA_name : S,
PDATA_value : S
] MkDataStr;;
struct LogStr = [
PLOG_inst : PInstance,
PLOG_bAddTime : I,
PLOG_bElapsedTime : I,
PLOG_fElapsedTime : F,
pLOG_iLastTick : I,
PLOG_file : W,
PLOG_sFileName : S,
PLOG_lParams : [DataStr r1],
PLOG_bState : I
] MkLogStr;;
/*! \brief Callback on instance destruction
*
* Prototype: fun [PInstance] I
*
* \param PInstance : destroyed plugIT instance
*
* \return I : 0
**/
fun deleteOb(inst, obstr)=
0;;
/*! \brief Add a line to the log file
*
* Prototype: fun [PInstance] I
*
* \param PInstance : Log file Instance
*
* \return I : 0
**/
fun writeLog(obstr, datastr)=
let strcatn datastr.PDATA_name::" > "::datastr.PDATA_value::nil -> logline in
(
// Check if elapsed time must be written to file
if (!obstr.PLOG_bElapsedTime) then nil else
let _tickcount - obstr.pLOG_iLastTick -> period in
(
set obstr.pLOG_iLastTick = _tickcount;
set obstr.PLOG_fElapsedTime = obstr.PLOG_fElapsedTime +. (1.0 /. (1000.0 /. (itof period)));
set logline = strcat (strcat ftoa (obstr.PLOG_fElapsedTime -. (itof period) /. 1000.0) " | ") logline;
);
// Check if system time must be written to file
if (!obstr.PLOG_bAddTime) then nil else
let nth_list (hd strextr (ctime time)) 3 -> stime in
set logline = strcat (strcat stime " | ") logline;
_appendpack (strcat logline "\n") obstr.PLOG_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, obstr)=
if (obstr.PLOG_bState) then nil else
(
set obstr.pLOG_iLastTick = _tickcount;
set obstr.PLOG_bState = 1;
);
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, obstr)=
set obstr.PLOG_bState = 0;
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, p)=
let p -> [obstr datastr] in
if (!obstr.PLOG_bState) then nil else
(
//protect possible \n
//let protectFormat param -> param in
set datastr.PDATA_value = strtrim param;
writeLog obstr datastr;
);
0;;
fun cbNewFile(inst, from, action, param, reply, obstr)=
let strcatn sLogFilePath::(strreplace (strcatnSep (hd strextr (ctime time)) "_") ":" "-")::"_"::(getShortName getPluginInstanceFullname inst)::".txt"::nil -> filename in
(
set obstr.PLOG_sFileName = filename;
_deletepack _checkpack filename;
set obstr.PLOG_file = _getmodifypack filename;
set obstr.PLOG_fElapsedTime = 0.0;
);
0;;
fun cbGetFileName(inst, from, action, param, reply, obstr)=
SendPluginEvent inst "File name" obstr.PLOG_sFileName nil;
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)=
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 "init") -> init in
let getPluginInstanceUserActions inst -> laction in
let strcatn sLogFilePath::(strreplace (strcatnSep (hd strextr (ctime time)) "_") ":" "-")::"_"::(getShortName getPluginInstanceFullname inst)::".txt"::nil -> filename in
let MkLogStr [inst addtime addelapsedtime 0.0 _tickcount nil filename nil init] -> obstr in
(
_deletepack _checkpack filename;
set obstr.PLOG_file = _getmodifypack filename;
// Associate callbacks to actions
PluginRegisterAction inst "Start" mkfun6 @cbStartLog obstr;
PluginRegisterAction inst "Stop" mkfun6 @cbStopLog obstr;
PluginRegisterAction inst "New file" mkfun6 @cbNewFile obstr;
PluginRegisterAction inst "Get file name" mkfun6 @cbGetFileName obstr;
let sizelist laction -> size in
let 0 -> i in
let nil -> lparams in
(
while (i < size) do
(
let nth_list laction i -> actname in
let MkDataStr [actname ""] -> datastr in
(
set lparams = lcat lparams datastr::nil;
PluginRegisterAction inst actname mkfun6 @cbChangeValue [obstr datastr];
);
set i = i + 1;
);
set obstr.PLOG_lParams = lparams;
);
// Define the callback to call when the plugin instance is deleted
setPluginInstanceCbDel inst mkfun2 @deleteOb obstr;
);
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;;