1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: simplelogger.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
34 #include "simplelogger.hxx"
35 #include <com/sun/star/util/logging/LogLevel.hpp>
37 #include <osl/thread.h>
38 #include <stdlib.h> // for getenv
43 namespace LogLevel
= com::sun::star::util::logging::LogLevel
;
45 //==============================================================================
46 static rtl::OString
level2str(sal_Int32 nLevel
)
48 return rtl::OString::valueOf(nLevel
);
51 sal_Int32
str2level(rtl::OString
const & str
)
56 //------------------------------------------------------------------------------
57 SimpleLogger::SimpleLogger( const uno::Reference
<uno::XComponentContext
>& xContext
, char const * name
)
59 , mName( OUString::createFromAscii(name
) )
61 , mLevel(LogLevel::INFO
)
63 if (char const * fname
= getenv("CFG_LOGFILE"))
65 mOutput
= fopen(fname
,"a+");
66 OSL_ENSURE(mOutput
,"ERROR: could not open logfile\n");
69 if (char const * level
= getenv("CFG_LOGLEVEL"))
71 mLevel
= str2level(level
);
74 //------------------------------------------------------------------------------
75 SimpleLogger::~SimpleLogger()
78 //------------------------------------------------------------------------------
79 uno::Reference
< logging::XLogger
> SAL_CALL
80 SimpleLogger::getLogger( const OUString
& name
)
81 throw (uno::RuntimeException
)
83 if (name
== mName
) return this;
86 uno::Reference
< logging::XLogger
> xNamedLogger
;
89 OUString
const singleton(RTL_CONSTASCII_USTRINGPARAM("/singletons/"));
90 mContext
->getValueByName(singleton
.concat(name
)) >>= xNamedLogger
;
95 //------------------------------------------------------------------------------
96 sal_Int32 SAL_CALL
SimpleLogger::getLevel( ) throw (uno::RuntimeException
)
101 //------------------------------------------------------------------------------
102 OUString SAL_CALL
SimpleLogger::getName( ) throw (uno::RuntimeException
)
107 //------------------------------------------------------------------------------
108 sal_Bool SAL_CALL
SimpleLogger::isLoggable( sal_Int32 level
) throw (uno::RuntimeException
)
110 return mOutput
&& level
>= mLevel
;
113 //------------------------------------------------------------------------------
114 #define OU2OUT( ustr ) ( rtl::OUStringToOString( ustr, enc ).getStr() )
116 void SAL_CALL
SimpleLogger::logp( sal_Int32 level
, const OUString
& sourceClass
, const OUString
& sourceMethod
, const OUString
& msg
)
117 throw (uno::RuntimeException
)
119 rtl_TextEncoding enc
= osl_getThreadTextEncoding();
120 if (mOutput
&& level
> mLevel
)
122 fprintf( mOutput
, "%s {%s.%s}: [%s] %s\n", OU2OUT(mName
),
123 OU2OUT(sourceClass
) , OU2OUT(sourceMethod
),
124 level2str(level
).getStr(), OU2OUT(msg
) );
128 //------------------------------------------------------------------------------
130 OUString SAL_CALL
SimpleLogger::getImplementationName_static()
132 static const char kImplementationName
[] = "com.sun.star.comp.configmgr.logging.SimpleLogger";
134 return OUString(RTL_CONSTASCII_USTRINGPARAM(kImplementationName
)) ;
136 //------------------------------------------------------------------------------
138 rtl::OUString SAL_CALL
SimpleLogger::getImplementationName()
139 throw (uno::RuntimeException
)
141 return getImplementationName_static() ;
143 //------------------------------------------------------------------------------
145 uno::Sequence
<rtl::OUString
> SAL_CALL
SimpleLogger::getSupportedServiceNames_static()
147 uno::Sequence
<rtl::OUString
> aServices(2) ;
148 aServices
[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.logging.SimpleLogger")) ;
149 aServices
[1] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.util.logging.Logger")) ;
153 //------------------------------------------------------------------------------
155 sal_Bool SAL_CALL
SimpleLogger::supportsService(
156 const rtl::OUString
& aServiceName
)
157 throw (uno::RuntimeException
)
159 uno::Sequence
< rtl::OUString
> const svc
= getSupportedServiceNames_static();
161 for(sal_Int32 i
= 0; i
< svc
.getLength(); ++i
)
162 if(svc
[i
] == aServiceName
)
166 //------------------------------------------------------------------------------
168 uno::Sequence
<rtl::OUString
>
169 SAL_CALL
SimpleLogger::getSupportedServiceNames()
170 throw (uno::RuntimeException
)
172 return getSupportedServiceNames_static() ;
174 //------------------------------------------------------------------------------
177 } // namespace logger