@@ -974,7 +974,7 @@ static SLONG safe_interpret(char* const s, const FB_SIZE_T bufsize,
974
974
}
975
975
976
976
if (!found) {
977
- sprintf (s, " unknown ISC error %ld" , code); // TXNN
977
+ sprintf (s, " unknown ISC error %ld" , (SLONG) code); // TXNN
978
978
}
979
979
}
980
980
}
@@ -999,11 +999,11 @@ static SLONG safe_interpret(char* const s, const FB_SIZE_T bufsize,
999
999
break ;
1000
1000
1001
1001
case isc_arg_dos:
1002
- sprintf (s, " unknown dos error %ld" , code); // TXNN
1002
+ sprintf (s, " unknown dos error %ld" , (SLONG) code); // TXNN
1003
1003
break ;
1004
1004
1005
1005
case isc_arg_next_mach:
1006
- sprintf (s, " next/mach error %ld" , code); // AP
1006
+ sprintf (s, " next/mach error %ld" , (SLONG) code); // AP
1007
1007
break ;
1008
1008
1009
1009
case isc_arg_win32:
@@ -1015,7 +1015,7 @@ static SLONG safe_interpret(char* const s, const FB_SIZE_T bufsize,
1015
1015
s, bufsize, NULL ))
1016
1016
#endif
1017
1017
{
1018
- sprintf (s, " unknown Win32 error %ld" , code); // TXNN
1018
+ sprintf (s, " unknown Win32 error %ld" , (SLONG) code); // TXNN
1019
1019
}
1020
1020
break ;
1021
1021
@@ -1083,80 +1083,122 @@ const int SECS_PER_HOUR = 60 * 60;
1083
1083
const int SECS_PER_DAY = SECS_PER_HOUR * 24 ;
1084
1084
1085
1085
#ifdef WIN_NT
1086
- class CleanupTraceHandles
1086
+
1087
+ namespace {
1088
+
1089
+ class LogFileHandles
1087
1090
{
1088
1091
public:
1089
- ~CleanupTraceHandles ()
1092
+ LogFileHandles (Firebird::MemoryPool&)
1093
+ {
1094
+ mutex_handle = CreateMutex (ISC_get_security_desc (), FALSE , " firebird_trace_mutex" );
1095
+ }
1096
+
1097
+ ~LogFileHandles ()
1090
1098
{
1091
- CloseHandle (trace_mutex_handle);
1092
- trace_mutex_handle = INVALID_HANDLE_VALUE ;
1099
+ if (mutex_handle != INVALID_HANDLE_VALUE)
1100
+ CloseHandle (mutex_handle) ;
1093
1101
1094
- if (trace_file_handle != INVALID_HANDLE_VALUE)
1095
- CloseHandle (trace_file_handle);
1102
+ mutex_handle = INVALID_HANDLE_VALUE;
1096
1103
1097
- trace_file_handle = INVALID_HANDLE_VALUE;
1104
+ if (file_handle != INVALID_HANDLE_VALUE)
1105
+ CloseHandle (file_handle);
1106
+
1107
+ file_handle = INVALID_HANDLE_VALUE;
1098
1108
}
1099
1109
1110
+ void trace_raw (const char * text, unsigned int length);
1111
+
1112
+ private:
1100
1113
// This is machine-global. Can be made instance-global.
1101
1114
// For as long you don't trace two instances in parallel this shouldn't matter.
1102
- static HANDLE trace_mutex_handle;
1103
- static HANDLE trace_file_handle;
1104
- };
1105
-
1106
- HANDLE CleanupTraceHandles::trace_mutex_handle = CreateMutex(NULL , FALSE , " firebird_trace_mutex" );
1107
- HANDLE CleanupTraceHandles::trace_file_handle = INVALID_HANDLE_VALUE;
1115
+ static HANDLE mutex_handle;
1116
+ static HANDLE file_handle;
1108
1117
1109
- CleanupTraceHandles cleanupHandles;
1110
-
1111
- #endif
1118
+ friend class LogGuard ;
1119
+ };
1112
1120
1113
- void API_ROUTINE gds__trace_raw (const char * text, unsigned int length)
1121
+ void LogFileHandles::trace_raw (const char * text, unsigned int length)
1114
1122
{
1115
- /* *************************************
1116
- *
1117
- * g d s _ t r a c e _ r a w
1118
- *
1119
- **************************************
1120
- *
1121
- * Functional description
1122
- * Write trace event to a log file
1123
- *
1124
- **************************************/
1125
- if (!length)
1126
- length = static_cast <unsigned >(strlen (text));
1127
- #ifdef WIN_NT
1128
- // Note: thread-safe code
1129
-
1130
1123
// Nickolay Samofatov, 12 Sept 2003. Windows opens files extremely slowly.
1131
1124
// Slowly enough to make such trace useless. Thus we cache file handle !
1132
- WaitForSingleObject (CleanupTraceHandles::trace_mutex_handle, INFINITE);
1125
+
1133
1126
while (true )
1134
1127
{
1135
- if (CleanupTraceHandles::trace_file_handle == INVALID_HANDLE_VALUE)
1128
+ if (file_handle == INVALID_HANDLE_VALUE)
1136
1129
{
1137
1130
Firebird::PathName name = fb_utils::getPrefix (Firebird::IConfigManager::DIR_LOG, LOGFILE);
1131
+
1138
1132
// We do not care to close this file.
1139
1133
// It will be closed automatically when our process terminates.
1140
- CleanupTraceHandles::trace_file_handle = CreateFile (name.c_str (), GENERIC_WRITE,
1134
+ file_handle = CreateFile (name.c_str (), GENERIC_WRITE,
1141
1135
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1142
1136
NULL , OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
1143
- if (CleanupTraceHandles::trace_file_handle == INVALID_HANDLE_VALUE)
1137
+
1138
+ if (file_handle == INVALID_HANDLE_VALUE)
1144
1139
break ;
1145
1140
}
1141
+
1142
+ SetFilePointer (file_handle, 0 , NULL , FILE_END);
1143
+
1146
1144
DWORD bytesWritten;
1147
- SetFilePointer (CleanupTraceHandles::trace_file_handle, 0 , NULL , FILE_END );
1148
- WriteFile (CleanupTraceHandles::trace_file_handle, text, length, &bytesWritten, NULL );
1145
+ WriteFile (file_handle, text, length, &bytesWritten, NULL );
1146
+
1149
1147
if (bytesWritten != length)
1150
1148
{
1151
1149
// Handle the case when file was deleted by another process on Win9x
1152
1150
// On WinNT we are not going to notice that fact :(
1153
- CloseHandle (CleanupTraceHandles::trace_file_handle );
1154
- CleanupTraceHandles::trace_file_handle = INVALID_HANDLE_VALUE;
1151
+ CloseHandle (file_handle );
1152
+ file_handle = INVALID_HANDLE_VALUE;
1155
1153
continue ;
1156
1154
}
1157
1155
break ;
1158
1156
}
1159
- ReleaseMutex (CleanupTraceHandles::trace_mutex_handle);
1157
+ }
1158
+
1159
+ Firebird::InitInstance<LogFileHandles> logFileHandles;
1160
+
1161
+ HANDLE LogFileHandles::mutex_handle = INVALID_HANDLE_VALUE;
1162
+ HANDLE LogFileHandles::file_handle = INVALID_HANDLE_VALUE;
1163
+
1164
+
1165
+ class LogGuard
1166
+ {
1167
+ public:
1168
+ LogGuard ()
1169
+ {
1170
+ WaitForSingleObject (logFileHandles ().mutex_handle , INFINITE);
1171
+ }
1172
+
1173
+ ~LogGuard ()
1174
+ {
1175
+ ReleaseMutex (logFileHandles ().mutex_handle );
1176
+ }
1177
+ };
1178
+
1179
+ } // namespace
1180
+
1181
+ #endif
1182
+
1183
+ void API_ROUTINE gds__trace_raw (const char * text, unsigned int length)
1184
+ {
1185
+ /* *************************************
1186
+ *
1187
+ * g d s _ t r a c e _ r a w
1188
+ *
1189
+ **************************************
1190
+ *
1191
+ * Functional description
1192
+ * Write trace event to a log file
1193
+ *
1194
+ **************************************/
1195
+ if (!length)
1196
+ length = static_cast <unsigned >(strlen (text));
1197
+ #ifdef WIN_NT
1198
+ // Note: thread-safe code
1199
+
1200
+ LogGuard guard;
1201
+ logFileHandles ().trace_raw (text, length);
1160
1202
#else
1161
1203
Firebird::PathName name = fb_utils::getPrefix (Firebird::IConfigManager::DIR_LOG, LOGFILE);
1162
1204
int file = os_utils::open (name.c_str (), O_CREAT | O_APPEND | O_WRONLY, 0660 );
@@ -1290,7 +1332,7 @@ void API_ROUTINE gds__log(const TEXT* text, ...)
1290
1332
Firebird::PathName name = fb_utils::getPrefix (Firebird::IConfigManager::DIR_LOG, LOGFILE);
1291
1333
1292
1334
#ifdef WIN_NT
1293
- WaitForSingleObject (CleanupTraceHandles::trace_mutex_handle, INFINITE) ;
1335
+ LogGuard guard ;
1294
1336
#endif
1295
1337
1296
1338
FILE* file = os_utils::fopen (name.c_str (), " a" );
@@ -1328,8 +1370,6 @@ void API_ROUTINE gds__log(const TEXT* text, ...)
1328
1370
va_start (ptr, text);
1329
1371
__android_log_vprint (ANDROID_LOG_INFO, " FIREBIRD" , text, ptr);
1330
1372
va_end (ptr);
1331
- #elif defined(WIN_NT)
1332
- ReleaseMutex (CleanupTraceHandles::trace_mutex_handle);
1333
1373
#endif
1334
1374
}
1335
1375
@@ -1362,7 +1402,7 @@ void gds__print_pool(MemoryPool* pool, const TEXT* text, ...)
1362
1402
1363
1403
const int oldmask = umask (0111 );
1364
1404
#ifdef WIN_NT
1365
- WaitForSingleObject (CleanupTraceHandles::trace_mutex_handle, INFINITE) ;
1405
+ LogGuard guard ;
1366
1406
#endif
1367
1407
FILE* file = os_utils::fopen (name.c_str (), " a" );
1368
1408
if (file != NULL )
@@ -1377,9 +1417,6 @@ void gds__print_pool(MemoryPool* pool, const TEXT* text, ...)
1377
1417
fprintf (file, " \n " );
1378
1418
fclose (file);
1379
1419
}
1380
- #ifdef WIN_NT
1381
- ReleaseMutex (CleanupTraceHandles::trace_mutex_handle);
1382
- #endif
1383
1420
1384
1421
umask (oldmask);
1385
1422
0 commit comments