summaryrefslogtreecommitdiff
path: root/sal
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2013-11-14 10:14:18 +0100
committerStephan Bergmann <sbergman@redhat.com>2013-11-14 10:15:16 +0100
commit7377aaa1aec90f105b6c5d56a2c9453081b2b8ba (patch)
tree7ce046a07e8a2a442741138d5770c5657a423f3b /sal
parent388734a14fbb13bf5960d80801d9bf38a1fb408a (diff)
Use rtl::Static
Change-Id: I391e027b2567c4239c1d02e132c6508b3f49d381
Diffstat (limited to 'sal')
-rw-r--r--sal/rtl/logfile.cxx217
1 files changed, 97 insertions, 120 deletions
diff --git a/sal/rtl/logfile.cxx b/sal/rtl/logfile.cxx
index 8bcef426710f..a78eb084ffab 100644
--- a/sal/rtl/logfile.cxx
+++ b/sal/rtl/logfile.cxx
@@ -49,48 +49,6 @@ using ::rtl::OUStringBuffer;
namespace {
-static oslFileHandle g_aFile = 0;
-static sal_Bool g_bHasBeenCalled = sal_False;
-static const sal_Int32 g_BUFFERSIZE = 4096;
-static sal_Char *g_buffer = 0;
-
-class LoggerGuard
-{
-public:
- ~LoggerGuard();
-};
-
-LoggerGuard::~LoggerGuard()
-{
- if( g_buffer )
- {
- sal_Int64 nWritten, nConverted =
- sprintf( g_buffer, "closing log file at %06" SAL_PRIuUINT32, osl_getGlobalTimer() );
- if( nConverted > 0 )
- osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64 *)&nWritten );
- osl_closeFile( g_aFile );
- g_aFile = 0;
-
- rtl_freeMemory( g_buffer );
- g_buffer = 0;
- g_bHasBeenCalled = sal_False;
- }
-}
-
-// The destructor of this static LoggerGuard is "activated" by the assignment to
-// g_buffer in init():
-LoggerGuard loggerGuard;
-
-namespace
-{
- class theLogMutex : public rtl::Static<osl::Mutex, theLogMutex>{};
-}
-
-static Mutex & getLogMutex()
-{
- return theLogMutex::get();
-}
-
OUString getFileUrl( const OUString &name )
{
OUString aRet;
@@ -108,124 +66,144 @@ OUString getFileUrl( const OUString &name )
return aRet;
}
-void init() {
- if( !g_bHasBeenCalled )
+static const sal_Int32 g_BUFFERSIZE = 4096;
+
+struct Logger {
+ oslFileHandle aFile;
+ sal_Char *buffer;
+ osl::Mutex mutex;
+
+ Logger();
+
+ ~Logger();
+};
+
+Logger::Logger(): aFile(0), buffer(0)
+{
+ OUString name( "RTL_LOGFILE" );
+ OUString value;
+ if( rtl_bootstrap_get( name.pData, &value.pData, 0 ) )
{
- MutexGuard guard( getLogMutex() );
- if( ! g_bHasBeenCalled )
+ // Obtain process id.
+ oslProcessIdentifier aProcessId = 0;
+ oslProcessInfo info;
+ info.Size = sizeof (oslProcessInfo);
+ if (osl_getProcessInfo (0, osl_Process_IDENTIFIER, &info) == osl_Process_E_None)
+ aProcessId = info.Ident;
+
+ // Construct name of log file and open the file.
+ OUStringBuffer buf( 128 );
+ buf.append( value );
+
+ // if the filename ends with .nopid, the incoming filename is not modified
+ if( value.getLength() < 6 /* ".nopid" */ ||
+ rtl_ustr_ascii_compare_WithLength(
+ value.getStr() + (value.getLength()-6) , 6 , ".nopid" ) )
{
- OUString name( "RTL_LOGFILE" );
- OUString value;
- if( rtl_bootstrap_get( name.pData, &value.pData, 0 ) )
- {
- // Obtain process id.
- oslProcessIdentifier aProcessId = 0;
- oslProcessInfo info;
- info.Size = sizeof (oslProcessInfo);
- if (osl_getProcessInfo (0, osl_Process_IDENTIFIER, &info) == osl_Process_E_None)
- aProcessId = info.Ident;
-
- // Construct name of log file and open the file.
- OUStringBuffer buf( 128 );
- buf.append( value );
-
- // if the filename ends with .nopid, the incoming filename is not modified
- if( value.getLength() < 6 /* ".nopid" */ ||
- rtl_ustr_ascii_compare_WithLength(
- value.getStr() + (value.getLength()-6) , 6 , ".nopid" ) )
- {
- buf.appendAscii( "_" );
- buf.append( (sal_Int32) aProcessId );
- buf.appendAscii( ".log" );
- }
+ buf.appendAscii( "_" );
+ buf.append( (sal_Int32) aProcessId );
+ buf.appendAscii( ".log" );
+ }
- OUString o = getFileUrl( buf.makeStringAndClear() );
- oslFileError e = osl_openFile(
- o.pData, &g_aFile, osl_File_OpenFlag_Write|osl_File_OpenFlag_Create);
+ OUString o = getFileUrl( buf.makeStringAndClear() );
+ oslFileError e = osl_openFile(
+ o.pData, &aFile, osl_File_OpenFlag_Write|osl_File_OpenFlag_Create);
- if( osl_File_E_None == e )
- {
- TimeValue aCurrentTime;
- g_buffer = ( sal_Char * ) rtl_allocateMemory( g_BUFFERSIZE );
- sal_Int64 nConverted = 0;
- if (osl_getSystemTime (&aCurrentTime))
- {
- nConverted = (sal_Int64 ) sprintf (
- g_buffer,
- "opening log file %f seconds past January 1st 1970\n"
- "corresponding to %" SAL_PRIuUINT32 " ms after timer start\n",
- aCurrentTime.Seconds + 1e-9 * aCurrentTime.Nanosec,
- osl_getGlobalTimer());
-
- if( nConverted > 0 )
- {
- sal_Int64 nWritten;
- osl_writeFile( g_aFile, g_buffer, nConverted , (sal_uInt64 *)&nWritten );
- }
- }
-
- nConverted = sprintf (g_buffer, "Process id is %" SAL_PRIuUINT32 "\n", aProcessId);
- if( nConverted )
- {
- sal_Int64 nWritten;
- osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64 *)&nWritten );
- }
- }
- else
+ if( osl_File_E_None == e )
+ {
+ TimeValue aCurrentTime;
+ buffer = ( sal_Char * ) rtl_allocateMemory( g_BUFFERSIZE );
+ sal_Int64 nConverted = 0;
+ if (osl_getSystemTime (&aCurrentTime))
+ {
+ nConverted = (sal_Int64 ) sprintf (
+ buffer,
+ "opening log file %f seconds past January 1st 1970\n"
+ "corresponding to %" SAL_PRIuUINT32 " ms after timer start\n",
+ aCurrentTime.Seconds + 1e-9 * aCurrentTime.Nanosec,
+ osl_getGlobalTimer());
+
+ if( nConverted > 0 )
{
- SAL_WARN(
- "sal.rtl",
- "Couldn't open logfile " << o << '(' << +e << ')');
+ sal_Int64 nWritten;
+ osl_writeFile( aFile, buffer, nConverted , (sal_uInt64 *)&nWritten );
}
}
- g_bHasBeenCalled = sal_True;
+
+ nConverted = sprintf (buffer, "Process id is %" SAL_PRIuUINT32 "\n", aProcessId);
+ if( nConverted )
+ {
+ sal_Int64 nWritten;
+ osl_writeFile( aFile, buffer, nConverted, (sal_uInt64 *)&nWritten );
+ }
+ }
+ else
+ {
+ SAL_WARN(
+ "sal.rtl",
+ "Couldn't open logfile " << o << '(' << +e << ')');
}
}
}
+Logger::~Logger()
+{
+ if( buffer )
+ {
+ sal_Int64 nWritten, nConverted =
+ sprintf( buffer, "closing log file at %06" SAL_PRIuUINT32, osl_getGlobalTimer() );
+ if( nConverted > 0 )
+ osl_writeFile( aFile, buffer, nConverted, (sal_uInt64 *)&nWritten );
+ osl_closeFile( aFile );
+ rtl_freeMemory( buffer );
+ }
+}
+
+struct theLogger: public rtl::Static<Logger, theLogger> {};
+
}
extern "C" void SAL_CALL rtl_logfile_trace ( const char *pszFormat, ... )
{
- init();
- if( g_buffer )
+ Logger & logger = theLogger::get();
+ if( logger.buffer )
{
va_list args;
va_start(args, pszFormat);
{
sal_Int64 nConverted, nWritten;
- MutexGuard guard( getLogMutex() );
- nConverted = vsnprintf( g_buffer , g_BUFFERSIZE, pszFormat, args );
+ MutexGuard guard( logger.mutex );
+ nConverted = vsnprintf( logger.buffer , g_BUFFERSIZE, pszFormat, args );
nConverted = (nConverted > g_BUFFERSIZE ? g_BUFFERSIZE : nConverted );
if( nConverted > 0 )
- osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64*)&nWritten );
+ osl_writeFile( logger.aFile, logger.buffer, nConverted, (sal_uInt64*)&nWritten );
}
va_end(args);
}
}
extern "C" void SAL_CALL rtl_logfile_longTrace(char const * format, ...) {
- init();
- if (g_buffer != 0) {
+ Logger & logger = theLogger::get();
+ if (logger.buffer != 0) {
sal_uInt32 time = osl_getGlobalTimer();
oslThreadIdentifier threadId = osl::Thread::getCurrentIdentifier();
va_list args;
va_start(args, format);
{
- MutexGuard g(getLogMutex());
+ MutexGuard g(logger.mutex);
int n1 = snprintf(
- g_buffer, g_BUFFERSIZE, "%06" SAL_PRIuUINT32 " %" SAL_PRIuUINT32 " ", time, threadId);
+ logger.buffer, g_BUFFERSIZE, "%06" SAL_PRIuUINT32 " %" SAL_PRIuUINT32 " ", time, threadId);
if (n1 >= 0) {
sal_uInt64 n2;
osl_writeFile(
- g_aFile, g_buffer,
+ logger.aFile, logger.buffer,
static_cast< sal_uInt64 >(
std::min(n1, static_cast< int >(g_BUFFERSIZE))),
&n2);
- n1 = vsnprintf(g_buffer, g_BUFFERSIZE, format, args);
+ n1 = vsnprintf(logger.buffer, g_BUFFERSIZE, format, args);
if (n1 > 0) {
osl_writeFile(
- g_aFile, g_buffer,
+ logger.aFile, logger.buffer,
static_cast< sal_uInt64 >(
std::min(n1, static_cast< int >(g_BUFFERSIZE))),
&n2);
@@ -237,8 +215,7 @@ extern "C" void SAL_CALL rtl_logfile_longTrace(char const * format, ...) {
}
extern "C" sal_Bool SAL_CALL rtl_logfile_hasLogFile( void ) {
- init();
- return g_buffer != 0;
+ return theLogger::get().buffer != 0;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */