File: _logging.c

package info (click to toggle)
unixodbc 2.3.6-0.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 11,476 kB
  • sloc: ansic: 92,148; sh: 11,928; makefile: 1,630; yacc: 969; sql: 1
file content (203 lines) | stat: -rw-r--r-- 4,830 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*********************************************************************
 *
 * Written by Nick Gorham
 * (nick@lurcher.org).
 *
 * copyright (c) 1999 Nick Gorham
 *
 * This library 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 library 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 **********************************************************************
 *
 * $Id: _logging.c,v 1.5 2009/02/18 17:59:27 lurcher Exp $
 *
 * $Log: _logging.c,v $
 * Revision 1.5  2009/02/18 17:59:27  lurcher
 * Shift to using config.h, the compile lines were making it hard to spot warnings
 *
 * Revision 1.4  2008/05/12 13:07:21  lurcher
 * Push a couple of small changes back into CVS, ready for new release
 *
 * Revision 1.3  2008/02/15 15:47:12  lurcher
 * Add thread protection around ini caching
 *
 * Revision 1.2  2007/11/27 17:52:57  peteralexharvey
 * - changes made during QT4 implementation
 *
 * Revision 1.1.1.1  2001/10/17 16:40:30  lurcher
 *
 * First upload to SourceForge
 *
 * Revision 1.1.1.1  2000/09/04 16:42:53  nick
 * Imported Sources
 *
 * Revision 1.1  1999/07/15 06:23:39  ngorham
 *
 * Added functions to remove the need for _init and _fini
 *
 *
 *********************************************************************/

#include <config.h>
#include <odbcinstext.h>
#include <log.h>

#ifdef HAVE_LIBPTH

#include <pth.h>

static pth_mutex_t mutex_log = PTH_MUTEX_INIT;
static int pth_init_called = 0;

static int local_mutex_entry( void )
{
    if ( !pth_init_called )
    {
        pth_init();
        pth_init_called = 1;
    }
    return pth_mutex_acquire( &mutex_log, 0, NULL );
}

static int local_mutex_exit( void )
{
    return pth_mutex_release( &mutex_log );
}

#elif HAVE_LIBPTHREAD

#include <pthread.h>

static pthread_mutex_t mutex_log = PTHREAD_MUTEX_INITIALIZER;

static int local_mutex_entry( void )
{
    return pthread_mutex_lock( &mutex_log );
}

static int local_mutex_exit( void )
{
    return pthread_mutex_unlock( &mutex_log );
}

#elif HAVE_LIBTHREAD

#include <thread.h>

static mutex_t mutex_log;

static int local_mutex_entry( void )
{
    return mutex_lock( &mutex_log );
}

static int local_mutex_exit( void )
{
    return mutex_unlock( &mutex_log );
}

#else

#define local_mutex_entry()
#define local_mutex_exit()

#endif
/*
 * I don't like these statics but not sure what else we can do...
 *
 * Indeed, access to these statics was in fact not thread safe !
 * So they are now protected by mutex_log...
 */

static HLOG hODBCINSTLog = NULL;
static int log_tried = 0;

int inst_logPushMsg( char *pszModule, char *pszFunctionName, int nLine, int nSeverity, int nCode, char *pszMessage )
{
    int ret = LOG_ERROR;

    local_mutex_entry();

    if ( !log_tried )
    {
        long nMaxMessages = 10; /* \todo ODBC spec says 8 max. We would make it 0 (unlimited) but at the moment logPeekMsg 
                                   would be slow if many messages. Revisit when opt is made to log storage. */

        log_tried = 1;
        if ( logOpen( &hODBCINSTLog, "odbcinst", NULL, nMaxMessages ) != LOG_SUCCESS )
        {
            hODBCINSTLog = NULL;
        }
        else
        {
            logOn( hODBCINSTLog, 1 );
        }
    }
    if ( hODBCINSTLog )
    {
        ret = logPushMsg( hODBCINSTLog,
                pszModule, 
                pszFunctionName, 
                nLine, 
                nSeverity, 
                nCode, 
                pszMessage );
    }

    local_mutex_exit();

    return ret;
}

/*! 
 * \brief   Get a reference to a message in the log.
 *
 *          The caller (SQLInstallerError) could call logPeekMsg directly
 *          but we would have to extern hODBCINSTLog and I have not given
 *          any thought to that at this time.
 * 
 * \param   nMsg
 * \param   phMsg
 * 
 * \return  int
 */
int inst_logPeekMsg( long nMsg, HLOGMSG *phMsg )
{
    int ret = LOG_NO_DATA;

    local_mutex_entry();

    if ( hODBCINSTLog )
        ret = logPeekMsg( hODBCINSTLog, nMsg, phMsg );

    local_mutex_exit();

    return ret;
}

int inst_logClear( void )
{
    int ret = LOG_ERROR;

    local_mutex_entry();

    if ( hODBCINSTLog ) 
        ret = logClear( hODBCINSTLog );

    local_mutex_exit();

    return ret;
}