Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / comphelper / source / misc / documentiologring.cxx
blobac9ba8ddfbf76b385baddd41dd8f1285f52db787
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <com/sun/star/frame/DoubleInitializationException.hpp>
22 #include <com/sun/star/lang/IllegalArgumentException.hpp>
24 #include <comphelper_module.hxx>
26 #include "documentiologring.hxx"
28 using namespace ::com::sun::star;
30 namespace comphelper
33 // ----------------------------------------------------------
34 OSimpleLogRing::OSimpleLogRing()
35 : m_aMessages( SIMPLELOGRING_SIZE )
36 , m_bInitialized( sal_False )
37 , m_bFull( sal_False )
38 , m_nPos( 0 )
42 // ----------------------------------------------------------
43 OSimpleLogRing::~OSimpleLogRing()
47 // ----------------------------------------------------------
48 uno::Sequence< ::rtl::OUString > SAL_CALL OSimpleLogRing::getSupportedServiceNames_static()
50 uno::Sequence< rtl::OUString > aResult( 1 );
51 aResult[0] = getServiceName_static();
52 return aResult;
55 // ----------------------------------------------------------
56 ::rtl::OUString SAL_CALL OSimpleLogRing::getImplementationName_static()
58 return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.logging.SimpleLogRing" ) );
61 // ----------------------------------------------------------
62 ::rtl::OUString SAL_CALL OSimpleLogRing::getSingletonName_static()
64 return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.DocumentIOLogRing" ) );
67 // ----------------------------------------------------------
68 ::rtl::OUString SAL_CALL OSimpleLogRing::getServiceName_static()
70 return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.SimpleLogRing" ) );
73 // ----------------------------------------------------------
74 uno::Reference< uno::XInterface > SAL_CALL OSimpleLogRing::Create( SAL_UNUSED_PARAMETER const uno::Reference< uno::XComponentContext >& )
76 return static_cast< cppu::OWeakObject* >( new OSimpleLogRing );
79 // XSimpleLogRing
80 // ----------------------------------------------------------
81 void SAL_CALL OSimpleLogRing::logString( const ::rtl::OUString& aMessage ) throw (uno::RuntimeException)
83 ::osl::MutexGuard aGuard( m_aMutex );
85 m_aMessages[m_nPos] = aMessage;
86 if ( ++m_nPos >= m_aMessages.getLength() )
88 m_nPos = 0;
89 m_bFull = sal_True;
92 // if used once then default initialized
93 m_bInitialized = sal_True;
96 // ----------------------------------------------------------
97 uno::Sequence< ::rtl::OUString > SAL_CALL OSimpleLogRing::getCollectedLog() throw (uno::RuntimeException)
99 ::osl::MutexGuard aGuard( m_aMutex );
101 sal_Int32 nResLen = m_bFull ? m_aMessages.getLength() : m_nPos;
102 sal_Int32 nStart = m_bFull ? m_nPos : 0;
103 uno::Sequence< ::rtl::OUString > aResult( nResLen );
105 for ( sal_Int32 nInd = 0; nInd < nResLen; nInd++ )
106 aResult[nInd] = m_aMessages[ ( nStart + nInd ) % m_aMessages.getLength() ];
108 // if used once then default initialized
109 m_bInitialized = sal_True;
111 return aResult;
114 // XInitialization
115 // ----------------------------------------------------------
116 void SAL_CALL OSimpleLogRing::initialize( const uno::Sequence< uno::Any >& aArguments ) throw (uno::Exception, uno::RuntimeException)
118 ::osl::MutexGuard aGuard( m_aMutex );
119 if ( m_bInitialized )
120 throw frame::DoubleInitializationException();
122 if ( !m_refCount )
123 throw uno::RuntimeException(); // the object must be refcounted already!
125 sal_Int32 nLen = 0;
126 if ( aArguments.getLength() == 1 && ( aArguments[0] >>= nLen ) && nLen )
127 m_aMessages.realloc( nLen );
128 else
129 throw lang::IllegalArgumentException(
130 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Nonnull size is expected as the first argument!" ) ),
131 uno::Reference< uno::XInterface >(),
132 0 );
134 m_bInitialized = sal_True;
137 // XServiceInfo
138 // ----------------------------------------------------------
139 ::rtl::OUString SAL_CALL OSimpleLogRing::getImplementationName() throw (uno::RuntimeException)
141 return getImplementationName_static();
144 // ----------------------------------------------------------
145 ::sal_Bool SAL_CALL OSimpleLogRing::supportsService( const ::rtl::OUString& aServiceName ) throw (uno::RuntimeException)
147 const uno::Sequence< rtl::OUString > & aSupportedNames = getSupportedServiceNames_static();
148 for ( sal_Int32 nInd = 0; nInd < aSupportedNames.getLength(); nInd++ )
150 if ( aSupportedNames[ nInd ].equals( aServiceName ) )
151 return sal_True;
154 return sal_False;
157 // ----------------------------------------------------------
158 uno::Sequence< ::rtl::OUString > SAL_CALL OSimpleLogRing::getSupportedServiceNames() throw (uno::RuntimeException)
160 return getSupportedServiceNames_static();
163 } // namespace comphelper
165 void createRegistryInfo_OSimpleLogRing()
167 static ::comphelper::module::OAutoRegistration< ::comphelper::OSimpleLogRing > aAutoRegistration;
168 static ::comphelper::module::OSingletonRegistration< ::comphelper::OSimpleLogRing > aSingletonRegistration;
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */