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