1/*********************************************************************
2 *
3 * Written by Nick Gorham
4 * (nick@lurcher.org).
5 *
6 * copyright (c) 1999 Nick Gorham
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 **********************************************************************
23 *
24 * $Id: _logging.c,v 1.5 2009/02/18 17:59:27 lurcher Exp $
25 *
26 * $Log: _logging.c,v $
27 * Revision 1.5 2009/02/18 17:59:27 lurcher
28 * Shift to using config.h, the compile lines were making it hard to spot warnings
29 *
30 * Revision 1.4 2008/05/12 13:07:21 lurcher
31 * Push a couple of small changes back into CVS, ready for new release
32 *
33 * Revision 1.3 2008/02/15 15:47:12 lurcher
34 * Add thread protection around ini caching
35 *
36 * Revision 1.2 2007/11/27 17:52:57 peteralexharvey
37 * - changes made during QT4 implementation
38 *
39 * Revision 1.1.1.1 2001/10/17 16:40:30 lurcher
40 *
41 * First upload to SourceForge
42 *
43 * Revision 1.1.1.1 2000/09/04 16:42:53 nick
44 * Imported Sources
45 *
46 * Revision 1.1 1999/07/15 06:23:39 ngorham
47 *
48 * Added functions to remove the need for _init and _fini
49 *
50 *
51 *********************************************************************/
52
53#include <config.h>
54#include <odbcinstext.h>
55#include <log.h>
56
57#ifdef HAVE_LIBPTH
58
59#include <pth.h>
60
61static pth_mutex_t mutex_log = PTH_MUTEX_INIT;
62static int pth_init_called = 0;
63
64static int local_mutex_entry( void )
65{
66 if ( !pth_init_called )
67 {
68 pth_init();
69 pth_init_called = 1;
70 }
71 return pth_mutex_acquire( &mutex_log, 0, NULL );
72}
73
74static int local_mutex_exit( void )
75{
76 return pth_mutex_release( &mutex_log );
77}
78
79#elif HAVE_LIBPTHREAD
80
81#include <pthread.h>
82
83static pthread_mutex_t mutex_log = PTHREAD_MUTEX_INITIALIZER;
84
85static int local_mutex_entry( void )
86{
87 return pthread_mutex_lock( &mutex_log );
88}
89
90static int local_mutex_exit( void )
91{
92 return pthread_mutex_unlock( &mutex_log );
93}
94
95#elif HAVE_LIBTHREAD
96
97#include <thread.h>
98
99static mutex_t mutex_log;
100
101static int local_mutex_entry( void )
102{
103 return mutex_lock( &mutex_log );
104}
105
106static int local_mutex_exit( void )
107{
108 return mutex_unlock( &mutex_log );
109}
110
111#else
112
113#define local_mutex_entry()
114#define local_mutex_exit()
115
116#endif
117/*
118 * I don't like these statics but not sure what else we can do...
119 *
120 * Indeed, access to these statics was in fact not thread safe !
121 * So they are now protected by mutex_log...
122 */
123
124static HLOG hODBCINSTLog = NULL;
125static int log_tried = 0;
126
127int inst_logPushMsg( char *pszModule, char *pszFunctionName, int nLine, int nSeverity, int nCode, char *pszMessage )
128{
129 int ret = LOG_ERROR;
130
131 local_mutex_entry();
132
133 if ( !log_tried )
134 {
135 long nMaxMessages = 10; /* \todo ODBC spec says 8 max. We would make it 0 (unlimited) but at the moment logPeekMsg
136 would be slow if many messages. Revisit when opt is made to log storage. */
137
138 log_tried = 1;
139 if ( logOpen( &hODBCINSTLog, "odbcinst", NULL, nMaxMessages ) != LOG_SUCCESS )
140 {
141 hODBCINSTLog = NULL;
142 }
143 else
144 {
145 logOn( hODBCINSTLog, 1 );
146 }
147 }
148 if ( hODBCINSTLog )
149 {
150 ret = logPushMsg( hODBCINSTLog,
151 pszModule,
152 pszFunctionName,
153 nLine,
154 nSeverity,
155 nCode,
156 pszMessage );
157 }
158
159 local_mutex_exit();
160
161 return ret;
162}
163
164/*!
165 * \brief Get a reference to a message in the log.
166 *
167 * The caller (SQLInstallerError) could call logPeekMsg directly
168 * but we would have to extern hODBCINSTLog and I have not given
169 * any thought to that at this time.
170 *
171 * \param nMsg
172 * \param phMsg
173 *
174 * \return int
175 */
176int inst_logPeekMsg( long nMsg, HLOGMSG *phMsg )
177{
178 int ret = LOG_NO_DATA;
179
180 local_mutex_entry();
181
182 if ( hODBCINSTLog )
183 ret = logPeekMsg( hODBCINSTLog, nMsg, phMsg );
184
185 local_mutex_exit();
186
187 return ret;
188}
189
190int inst_logClear( void )
191{
192 int ret = LOG_ERROR;
193
194 local_mutex_entry();
195
196 if ( hODBCINSTLog )
197 ret = logClear( hODBCINSTLog );
198
199 local_mutex_exit();
200
201 return ret;
202}
203
204