Crossfire Server, Trunk  R20513
Macros | Functions | Variables
cflogger.c File Reference

This plugin will log events to an sqlite3 database named cflogger.db in the var directory. More...

#include <cflogger.h>
#include <cflogger_proto.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
#include <svnversion.h>
Include dependency graph for cflogger.c:

Go to the source code of this file.

Macros

#define CFLOGGER_CURRENT_FORMAT   3
 Current database format. More...
 
#define DO_OR_ROLLBACK(sqlstring)
 Helper macros for rolling back and returning if query failed. More...
 
#define UPDATE_OR_ROLLBACK(tbl, newschema, select_columns)
 

Functions

static void add_death (object *victim, object *killer)
 Logs a death. More...
 
static void add_map_event (mapstruct *map, int event_code, object *pl)
 Logs an event for a map. More...
 
static void add_player_event (object *pl, int event_code)
 Logs an event for a living object. More...
 
CF_PLUGIN int cflogger_globalEventListener (int *type,...)
 Handles a global event. More...
 
CF_PLUGIN int cflogger_runPluginCommand (object *op, char *params)
 Runs a plugin command. More...
 
static void check_tables (void)
 Checks the database format, and applies changes if old version. More...
 
static int check_tables_callback (void *param, int argc, char **argv, char **azColName)
 Simple callback to get an integer from a query. More...
 
CF_PLUGIN int closePlugin (void)
 Close the plugin. More...
 
static int do_sql (const char *sql)
 Helper function to run a SQL query. More...
 
CF_PLUGIN int eventListener (int *type,...)
 Handles an object-related event. More...
 
static int get_living_id (object *living)
 Returns a unique identifier for specified object. More...
 
static int get_map_id (mapstruct *map)
 Gets the unique identifier for a map. More...
 
static int get_region_id (region *reg)
 Gets the unique identifier for a region. More...
 
CF_PLUGIN void * getPluginProperty (int *type,...)
 Gets a plugin property. More...
 
CF_PLUGIN int initPlugin (const char *iversion, f_plug_api gethooksptr)
 Main plugin entry point. More...
 
CF_PLUGIN int postInitPlugin (void)
 Plugin was initialized, now to finish. More...
 
static int store_time (void)
 Stores a line to match current ingame and real time. More...
 
static int update_table_format (const char *table, const char *newschema, const char *select_columns)
 Updates a table to a new schema, used for when ALTER TABLE doesn't work. More...
 

Variables

static sqlite3 * database
 Pointer to the logging database. More...
 
static int last_stored_day = -1
 To keep track of stored ingame/real time matching. More...
 
CF_PLUGIN char SvnRevPlugin [] = SVN_REV
 

Detailed Description

This plugin will log events to an sqlite3 database named cflogger.db in the var directory.

Log includes:

Warning
The plugin will not check the database size, which can grow a lot.
Note
Thanks to sqlite's locking, it's possible to access the database through the command line even while the server is running.

Definition in file cflogger.c.

Macro Definition Documentation

#define CFLOGGER_CURRENT_FORMAT   3

Current database format.

Definition at line 58 of file cflogger.c.

Referenced by check_tables().

#define DO_OR_ROLLBACK (   sqlstring)
Value:
if (do_sql(sqlstring) != SQLITE_OK) { \
do_sql("rollback transaction;"); \
cf_log(llevError, " [%s] Logger database format update failed! Couldn't upgrade from format %d to fromat %d!. Won't log.\n", PLUGIN_NAME, format, CFLOGGER_CURRENT_FORMAT);\
sqlite3_close(database); \
database = NULL; \
return; \
}
Error, serious thing.
Definition: logger.h:11
static sqlite3 * database
Pointer to the logging database.
Definition: cflogger.c:61
void cf_log(LogLevel logLevel, const char *format,...)
Wrapper for LOG().
static int do_sql(const char *sql)
Helper function to run a SQL query.
Definition: cflogger.c:103
#define PLUGIN_NAME
Definition: cfanim.h:32
#define CFLOGGER_CURRENT_FORMAT
Current database format.
Definition: cflogger.c:58

Helper macros for rolling back and returning if query failed.

Used in check_tables().

Yes they are quite messy. The alternatives seemed worse.

Definition at line 181 of file cflogger.c.

Referenced by check_tables().

#define UPDATE_OR_ROLLBACK (   tbl,
  newschema,
  select_columns 
)
Value:
if (update_table_format((tbl), (newschema), (select_columns)) != SQLITE_OK) { \
do_sql("rollback transaction;"); \
cf_log(llevError, " [%s] Logger database format update failed! Couldn't upgrade from format %d to fromat %d!. Won't log.\n", PLUGIN_NAME, format, CFLOGGER_CURRENT_FORMAT);\
sqlite3_close(database); \
database = NULL; \
return; \
}
Error, serious thing.
Definition: logger.h:11
static sqlite3 * database
Pointer to the logging database.
Definition: cflogger.c:61
void cf_log(LogLevel logLevel, const char *format,...)
Wrapper for LOG().
static int do_sql(const char *sql)
Helper function to run a SQL query.
Definition: cflogger.c:103
static int update_table_format(const char *table, const char *newschema, const char *select_columns)
Updates a table to a new schema, used for when ALTER TABLE doesn&#39;t work.
Definition: cflogger.c:144
#define PLUGIN_NAME
Definition: cfanim.h:32
#define CFLOGGER_CURRENT_FORMAT
Current database format.
Definition: cflogger.c:58

Definition at line 190 of file cflogger.c.

Referenced by check_tables().

Function Documentation

static void add_death ( object victim,
object killer 
)
static

Logs a death.

If either of the parameters is NULL, or if neither is a PLAYER, nothing is logged.

Parameters
victimwho died.
killerwho killed.

Definition at line 517 of file cflogger.c.

References cf_object_get_object_property(), CFAPI_OBJECT_PROP_OWNER, do_sql(), get_living_id(), get_map_id(), obj::level, obj::map, PLAYER, and obj::type.

Referenced by cflogger_globalEventListener().

Here is the call graph for this function:

Here is the caller graph for this function:

static void add_map_event ( mapstruct map,
int  event_code,
object pl 
)
static

Logs an event for a map.

Parameters
mapmap for which to log an event.
event_codearbitrary event code.
plobject causing the event. Can be NULL.

Definition at line 493 of file cflogger.c.

References do_sql(), get_living_id(), get_map_id(), PLAYER, and obj::type.

Referenced by cflogger_globalEventListener().

Here is the call graph for this function:

Here is the caller graph for this function:

static void add_player_event ( object pl,
int  event_code 
)
static

Logs an event for a living object.

Parameters
plobject for which to log an event.
event_codearbitrary event code.

Definition at line 467 of file cflogger.c.

References do_sql(), get_living_id(), get_map_id(), and obj::map.

Referenced by cflogger_globalEventListener().

Here is the call graph for this function:

Here is the caller graph for this function:

CF_PLUGIN int cflogger_globalEventListener ( int *  type,
  ... 
)

Handles a global event.

Parameters
typeignored.
Returns
0

Definition at line 626 of file cflogger.c.

References add_death(), add_map_event(), add_player_event(), EVENT_BORN, EVENT_CLOCK, EVENT_GKILL, EVENT_KICK, EVENT_LOGIN, EVENT_LOGOUT, EVENT_MAPENTER, EVENT_MAPLEAVE, EVENT_MAPLOAD, EVENT_MAPRESET, EVENT_MAPUNLOAD, EVENT_MUZZLE, EVENT_PLAYER_DEATH, EVENT_REMOVE, pl::ob, and store_time().

Referenced by postInitPlugin().

Here is the call graph for this function:

Here is the caller graph for this function:

CF_PLUGIN int cflogger_runPluginCommand ( object op,
char *  params 
)

Runs a plugin command.

Doesn't do anything.

Parameters
opignored.
paramsignored.
Returns
-1.

Definition at line 602 of file cflogger.c.

static void check_tables ( void  )
static

Checks the database format, and applies changes if old version.

Definition at line 202 of file cflogger.c.

References cf_log(), CFLOGGER_CURRENT_FORMAT, check_tables_callback(), database, DO_OR_ROLLBACK, do_sql(), llevDebug, llevError, PLUGIN_NAME, and UPDATE_OR_ROLLBACK.

Referenced by postInitPlugin().

Here is the call graph for this function:

Here is the caller graph for this function:

static int check_tables_callback ( void *  param,
int  argc,
char **  argv,
char **  azColName 
)
static

Simple callback to get an integer from a query.

Parameters
paramuser-supplied data.
argcnumber of items.
argvvalues.
azColNamecolumn names.
Returns
always returns 0 to continue the execution.

Definition at line 81 of file cflogger.c.

Referenced by check_tables().

Here is the caller graph for this function:

CF_PLUGIN int closePlugin ( void  )

Close the plugin.

Closes the sqlite database.

Returns
0.

Definition at line 748 of file cflogger.c.

References cf_log(), database, llevInfo, and PLUGIN_VERSION.

Here is the call graph for this function:

static int do_sql ( const char *  sql)
static

Helper function to run a SQL query.

Will LOG() an error if the query fails.

Parameters
sqlquery to run.
Returns
SQLITE_OK if no error, other value if error.
Note
There is most likely no need to check return value unless you need to rollback a transaction or similar.

Definition at line 103 of file cflogger.c.

References cf_log(), database, llevError, and PLUGIN_NAME.

Referenced by add_death(), add_map_event(), add_player_event(), check_tables(), get_living_id(), get_map_id(), get_region_id(), store_time(), and update_table_format().

Here is the call graph for this function:

Here is the caller graph for this function:

CF_PLUGIN int eventListener ( int *  type,
  ... 
)

Handles an object-related event.

Doesn't do anything.

Parameters
typeignored.
Returns
0.
Todo:
build from current map's path, probably

Definition at line 614 of file cflogger.c.

static int get_living_id ( object living)
static

Returns a unique identifier for specified object.

Will insert an item in the table if required.

If the object is a player, only name is taken into account to generate an id.

Else, the object's level is taken into account, to distinguish monsters with the same name and different levels (special monsters, and such).

Parameters
livingobject to get identifier for.
Returns
unique identifier in the 'living' table.

Definition at line 326 of file cflogger.c.

References database, do_sql(), obj::level, obj::name, PLAYER, and obj::type.

Referenced by add_death(), add_map_event(), and add_player_event().

Here is the call graph for this function:

Here is the caller graph for this function:

static int get_map_id ( mapstruct map)
static

Gets the unique identifier for a map.

Will generate one if required.

Maps starting with '/random/' will all share the same identifier for the same region.

Parameters
mapmap for which an id is wanted. Must not be NULL.
Returns
unique map identifier.

Definition at line 396 of file cflogger.c.

References database, do_sql(), get_region_id(), mapdef::path, and mapdef::region.

Referenced by add_death(), add_map_event(), and add_player_event().

Here is the call graph for this function:

Here is the caller graph for this function:

static int get_region_id ( region reg)
static

Gets the unique identifier for a region.

Will generate one if required.

Parameters
regregion for which an id is wanted
Returns
unique region identifier, or 0 if reg is NULL.

Definition at line 360 of file cflogger.c.

References database, do_sql(), and regiondef::name.

Referenced by get_map_id().

Here is the call graph for this function:

Here is the caller graph for this function:

CF_PLUGIN void* getPluginProperty ( int *  type,
  ... 
)

Gets a plugin property.

Parameters
typeignored.
Returns
  • the name, if asked for 'Identification'.
  • the version, if asked for 'FullName'.
  • NULL else.

Definition at line 566 of file cflogger.c.

References PLUGIN_NAME, PLUGIN_VERSION, and snprintf.

CF_PLUGIN int initPlugin ( const char *  iversion,
f_plug_api  gethooksptr 
)

Main plugin entry point.

Plugin initialisation function.

Parameters
iversionserver version.
gethooksptrfunction to get hooks from.
Returns
always 0.

Definition at line 550 of file cflogger.c.

References cf_init_plugin(), cf_log(), llevInfo, and PLUGIN_VERSION.

Here is the call graph for this function:

CF_PLUGIN int postInitPlugin ( void  )
static int store_time ( void  )
static

Stores a line to match current ingame and real time.

Returns
1 if a line was inserted, 0 if the current ingame time was already logged.

Definition at line 429 of file cflogger.c.

References cf_get_time(), database, _timeofday::day, do_sql(), _timeofday::hour, last_stored_day, _timeofday::minute, _timeofday::month, snprintf, and _timeofday::year.

Referenced by cflogger_globalEventListener(), and postInitPlugin().

Here is the call graph for this function:

Here is the caller graph for this function:

static int update_table_format ( const char *  table,
const char *  newschema,
const char *  select_columns 
)
static

Updates a table to a new schema, used for when ALTER TABLE doesn't work.

(Such as when changing column constraints.)

Parameters
tableName of table.
newschemaThis is the new table format. Will be inserted into the parantheses of "create table table_name()".
select_columnsThis is inserted into "INSERT INTO table_name SELECT _ FROM ..." to allow changing order of columns, or skipping some. Normally it should be "*".
Warning
This function should only be used in check_tables() below.

No error checking is done. Also it is expected that appending an _old on the table name won't collide with anything.

Note that columns are expected to have same (or compatible) type, and be in the same order. Further both tables should have the same number of columns.

Returns
SQLITE_OK if no error, non-zero if error. This SHOULD be rollback any transaction this function is called in.

Definition at line 144 of file cflogger.c.

References do_sql().

Here is the call graph for this function:

Variable Documentation

sqlite3* database
static

Pointer to the logging database.

Definition at line 61 of file cflogger.c.

Referenced by check_tables(), closePlugin(), do_sql(), get_living_id(), get_map_id(), get_region_id(), postInitPlugin(), and store_time().

int last_stored_day = -1
static

To keep track of stored ingame/real time matching.

Definition at line 64 of file cflogger.c.

Referenced by store_time().

CF_PLUGIN char SvnRevPlugin[] = SVN_REV

Definition at line 55 of file cflogger.c.