MCS  0.3.3-alpha7
mcs::Event Class Reference

Hold informations about an event. More...

#include <mcs.hh>

Public Member Functions

int code ()
 Returns the event code. More...
 
string codemsg ()
 Return the code and the message, formatted as MCS_PRE CODE MESSAGE. More...
 
 Event (string atFile, unsigned int atLine, RetValue type, int code, string s1="", string s2="", string s3="")
 Constructor with three optional strings. More...
 
 Event (string atFile, unsigned int atLine, RetValue type, int code, string s1, int i1)
 Constructor with a string and an integer. More...
 
 Event (string atFile, unsigned int atLine, RetValue type, int code, int i1, string s1="")
 Constructor with an integer and a string. More...
 
 Event (string atFile, unsigned int atLine, RetValue type, int code, int i1, int i2)
 Constructor with two integers. More...
 
string file ()
 Source file name where the event occurred. More...
 
unsigned int line ()
 Source line number where the event occurred. More...
 
string msg ()
 Returns the message. More...
 
int subcode ()
 Return event subcode. More...
 
Eventsubcode (int subcode)
 Set a subcode and return a reference to the object itself. More...
 
RetValue type ()
 Return the event type. More...
 
string where ()
 Returns the place where the error occurred formatted as FILE:LINE (CODE). More...
 
 ~Event ()
 Destructor. More...
 

Static Public Member Functions

static string static_vmsg (unsigned int i)
 Return the msg in the i-th position of the vmsg array. More...
 

Static Public Attributes

static void(* custom_init_vmsg )(const char *vmsg[1000]) = NULL
 Pointer to a callback function to initialize custom messages in the vmsg static array. More...
 

Private Member Functions

void init (string atFile, unsigned int atLine, RetValue type, int code)
 

Static Private Member Functions

static void init_vmsg ()
 Initialize the vmsg array. More...
 

Private Attributes

char buf [1024]
 Temporary buffer. More...
 
string latFile
 Source file. More...
 
int latLine
 Line number. More...
 
int lcode
 Event code;. More...
 
string lmsg
 Message. More...
 
int lsubcode
 Event subcode, usually an external library message code;. More...
 
RetValue ltype
 Event type. More...
 

Static Private Attributes

static bool flInitialized = false
 Flag to tell if the init() method has been already called. More...
 
static const char * vmsg [1000]
 Static array of descriptive messages. More...
 

Friends

class UserThread
 

Detailed Description

Hold informations about an event.

An event occur in two cases:

  • when we want to log informations about something happened, for example a new user connected, a request made by users, etc...;
  • an error occur and an exception must be thrown.

An Event object contains several informations about the event itself:

  • the source file and line at which the event occurred;
  • an event code, that is a numeric code which identify the event;
  • an event subcode, which is usually the return value of an external library function;
  • an event type, which says if the event is just a message or an error (see the RetValue enumeration);
  • a descriptive message.

To create an Event object you should provide all these informations (source file name, source file line, event type and event code) using one of the constructors. You cannot specify the descriptive message directly in the constructor, instead there must be an entry in the Event.vmsg static array, whose index is given by the event code, which contain the message and eventually some "printf" format specifier to add information to the message. These extra information can be passed in the constructors, this is the reason why all constructors have the same first four parameters while the other changes, so that the Event class can be easily used in all situations.

The Event.vmsg array is static, and contains all MCS predefined messages. It must be initialized calling the init() method (actually called by the constructor). The event codes for the MCS messages are defined as symbols with the "MSG_" prefix.

To use this class with new user-defined messages you should follow this rule:

  • define new numeric symbols for any of your messages, starting from (MCS_LAST_MESSAGE_CODE + 1) for messages, from (MCS_LAST_ERROR_CODE + 1) for errors;
  • after you istantiated the MCS server, set the correct values in the Event.vmsg array so that it points to your message descriptions;

An example follows:

#define MY_MESSAGE MCS_LAST_MESSAGE_CODE + 1
#define MY_ERROR MCS_LAST_ERROR_CODE + 1
.....
void initialize_messages(char* vmsg[MCS_VMSG_SIZE]) {
vmsg[MY_MESSAGE] = "My message: %s";
vmsg[MY_ERROR ] = "My error message: %s";
}
.....
mcs::Event::custom_init_vmsg = &initialize_messages;

Note that for any event there must be a numeric code identifier and a message in the corresponding position in the Event.vmsg array.

The Event class has all private fields, so once you created the object you can't modify its content.

Note that in the constructors parameter list there are always the arguments (atFile, atLine, type), in this order. You can avoid specifying any of these values using one of the MCS_OK, MCS_WARN, MCS_ERROR, MCS_FATAL or MCS_ macros.

Definition at line 814 of file mcs.hh.

Constructor & Destructor Documentation

◆ Event() [1/4]

mcs::Event::Event ( string  atFile,
unsigned int  atLine,
RetValue  type,
int  code,
string  s1 = "",
string  s2 = "",
string  s3 = "" 
)

Constructor with three optional strings.

This constructor set the subcode to zero.

Parameters
atFileSource file at which the event occurred;
atLineSource file line at which the event occurred;
typeEvent type;
codeEvent code;
s1Optional first string to write in the message;
s2Optional second string to write in the message;
s3Optional third string to write in the message;

Definition at line 53 of file Event.cc.

◆ Event() [2/4]

mcs::Event::Event ( string  atFile,
unsigned int  atLine,
RetValue  type,
int  code,
string  s1,
int  i1 
)

Constructor with a string and an integer.

This constructor set the subcode equal to the second parameter.

See also
Event(string, unsigned int, RetValue, int, string, string, string).

Definition at line 61 of file Event.cc.

◆ Event() [3/4]

mcs::Event::Event ( string  atFile,
unsigned int  atLine,
RetValue  type,
int  code,
int  i1,
string  s1 = "" 
)

Constructor with an integer and a string.

This constructor set the subcode to zero.

See also
Event(string, unsigned int, RetValue, int, string, string, string).

Definition at line 70 of file Event.cc.

◆ Event() [4/4]

mcs::Event::Event ( string  atFile,
unsigned int  atLine,
RetValue  type,
int  code,
int  i1,
int  i2 
)

Constructor with two integers.

This constructor set the subcode equal to the second parameter.

See also
Event(string, unsigned int, RetValue, int, string, string, string).

Definition at line 78 of file Event.cc.

◆ ~Event()

mcs::Event::~Event ( )

Destructor.

Definition at line 89 of file Event.cc.

Member Function Documentation

◆ code()

int mcs::Event::code ( )

Returns the event code.

Definition at line 92 of file Event.cc.

◆ codemsg()

string mcs::Event::codemsg ( )

Return the code and the message, formatted as MCS_PRE CODE MESSAGE.

Definition at line 136 of file Event.cc.

◆ file()

string mcs::Event::file ( )

Source file name where the event occurred.

Definition at line 124 of file Event.cc.

◆ init_vmsg()

void mcs::Event::init_vmsg ( )
staticprivate

Initialize the vmsg array.

Definition at line 23 of file mcsmsg.cc.

◆ line()

unsigned int mcs::Event::line ( )

Source line number where the event occurred.

Definition at line 130 of file Event.cc.

◆ msg()

string mcs::Event::msg ( )

Returns the message.

Definition at line 104 of file Event.cc.

◆ static_vmsg()

string mcs::Event::static_vmsg ( unsigned int  i)
static

Return the msg in the i-th position of the vmsg array.

Definition at line 156 of file Event.cc.

◆ subcode() [1/2]

int mcs::Event::subcode ( )

Return event subcode.

Definition at line 145 of file Event.cc.

◆ subcode() [2/2]

Event & mcs::Event::subcode ( int  subcode)

Set a subcode and return a reference to the object itself.

In the Event class constructor there's no way to distinguish a parameter from the variable list from an optional subcode, so we couldn't insert the "subcode" parameter in the constructor. But this way throwing an exception with a subcode require the following code:

Event e(__FILE__, __LINE__, ERROR, MSG_***, a, b, c,);
e.subcode(sub);
throw e;

This method solve the problem returning a reference to the object itself:

throw MCS_ERROR(MSG_***, a, b, c).subcode(sub);

Definition at line 150 of file Event.cc.

◆ type()

RetValue mcs::Event::type ( )

Return the event type.

Definition at line 99 of file Event.cc.

◆ where()

string mcs::Event::where ( )

Returns the place where the error occurred formatted as FILE:LINE (CODE).

Definition at line 110 of file Event.cc.

Member Data Documentation

◆ buf

char mcs::Event::buf[1024]
private

Temporary buffer.

Definition at line 824 of file mcs.hh.

◆ custom_init_vmsg

void(* mcs::Event::custom_init_vmsg)(const char *vmsg[1000]) = NULL
static

Pointer to a callback function to initialize custom messages in the vmsg static array.

Definition at line 853 of file mcs.hh.

◆ flInitialized

bool mcs::Event::flInitialized = false
staticprivate

Flag to tell if the init() method has been already called.

Definition at line 847 of file mcs.hh.

◆ latFile

string mcs::Event::latFile
private

Source file.

Definition at line 818 of file mcs.hh.

◆ latLine

int mcs::Event::latLine
private

Line number.

Definition at line 821 of file mcs.hh.

◆ lcode

int mcs::Event::lcode
private

Event code;.

Definition at line 830 of file mcs.hh.

◆ lmsg

string mcs::Event::lmsg
private

Message.

Definition at line 827 of file mcs.hh.

◆ lsubcode

int mcs::Event::lsubcode
private

Event subcode, usually an external library message code;.

Definition at line 833 of file mcs.hh.

◆ ltype

RetValue mcs::Event::ltype
private

Event type.

Definition at line 836 of file mcs.hh.

◆ vmsg

const char * mcs::Event::vmsg
staticprivate

Static array of descriptive messages.

Definition at line 841 of file mcs.hh.


The documentation for this class was generated from the following files:

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha7
Documentation generated on Mon May 28 07:39:41 UTC 2018