1 | /********************************************************************** |
2 | * logOpen |
3 | * |
4 | * |
5 | * This code was created by Peter Harvey (mostly during Christmas 98/99). |
6 | * This code is LGPL. Please ensure that this message remains in future |
7 | * distributions and uses of this code (thats about all I get out of it). |
8 | * - Peter Harvey pharvey@codebydesign.com |
9 | * |
10 | **********************************************************************/ |
11 | |
12 | #include <config.h> |
13 | #include "log.h" |
14 | |
15 | /*! |
16 | * \brief Open (init) a log (hLog). |
17 | * |
18 | * This function must be called before any other in this API. |
19 | * _logFreeMsg is applied to the log to ensure that all message |
20 | * mem is freed. |
21 | * |
22 | * \param phLog Output. Log handle returned here. |
23 | * \param pszProgramName Input. Default program name. This is attached to |
24 | * each message when written to file. It is only used |
25 | * if a viable pszLogFile given. Can be NULL. |
26 | * \param pszLogFile Input. File name of log file. Can be NULL. |
27 | * \param nMaxMsgs Input. Max messages to store. When this limit is |
28 | * reached - oldest message will be deleted. This |
29 | * can be set to 0 to remove any limit. |
30 | * |
31 | * \return int |
32 | * \retval LOG_ERROR |
33 | * \retval LOG_SUCCESS |
34 | * |
35 | * \sa logClose |
36 | */ |
37 | int logOpen( HLOG *phLog, char *pszProgramName, char *pszLogFile, long nMaxMsgs ) |
38 | { |
39 | /* sanity check */ |
40 | if ( !phLog ) return LOG_ERROR; |
41 | |
42 | /* LOG STRUCT */ |
43 | *phLog = malloc( sizeof(LOG) ); |
44 | (*phLog)->nMaxMsgs = nMaxMsgs; |
45 | (*phLog)->hMessages = lstOpen(); |
46 | (*phLog)->bOn = 0; |
47 | (*phLog)->pszLogFile = NULL; |
48 | (*phLog)->pszProgramName = NULL; |
49 | |
50 | /* each msg will be freed when deleted by this (_logFreeMsg) callback */ |
51 | lstSetFreeFunc( (*phLog)->hMessages, _logFreeMsg ); |
52 | |
53 | /* PROGRAM NAME */ |
54 | if ( pszProgramName ) |
55 | (*phLog)->pszProgramName = (char *)strdup( pszProgramName ); |
56 | else |
57 | (*phLog)->pszProgramName = (char *)strdup( "UNKNOWN" ); |
58 | |
59 | /* LOG FILE */ |
60 | if ( pszLogFile ) |
61 | (*phLog)->pszLogFile = (char*)strdup( pszLogFile ); |
62 | |
63 | return LOG_SUCCESS; |
64 | } |
65 | |
66 | |
67 | |