add front demo in this project

This commit is contained in:
lnk
2025-06-20 16:20:59 +08:00
parent 768eebbc2b
commit e14e3f9678
208 changed files with 54655 additions and 114 deletions

View File

@@ -0,0 +1,119 @@
// -*- C++ -*-
// Module: Log4CPLUS
// File: appenderattachableimpl.h
// Created: 6/2001
// Author: Tad E. Smith
//
//
// Copyright 2001-2017 Tad E. Smith
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/** @file */
#ifndef LOG4CPLUS_HELPERS_APPENDER_ATTACHABLE_IMPL_HEADER_
#define LOG4CPLUS_HELPERS_APPENDER_ATTACHABLE_IMPL_HEADER_
#include <log4cplus/config.hxx>
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#include <log4cplus/tstring.h>
#include <log4cplus/helpers/pointer.h>
#include <log4cplus/spi/appenderattachable.h>
#include <log4cplus/thread/syncprims.h>
#include <memory>
#include <vector>
namespace log4cplus {
namespace helpers {
/**
* This Interface is for attaching Appenders to objects.
*/
class LOG4CPLUS_EXPORT AppenderAttachableImpl
: public log4cplus::spi::AppenderAttachable
{
public:
// Data
thread::Mutex appender_list_mutex;
// Ctors
AppenderAttachableImpl();
// Dtor
virtual ~AppenderAttachableImpl();
// Methods
/**
* Add an appender. If the appender is already in the list in
* won't be added again.
*/
virtual void addAppender(SharedAppenderPtr newAppender);
/**
* Get all previously added appenders as an vectory.
*/
virtual SharedAppenderPtrList getAllAppenders();
/**
* Look for an attached appender named as <code>name</code>.
*
* Return the appender with that name if in the list. Return null
* otherwise.
*/
virtual SharedAppenderPtr getAppender(const log4cplus::tstring& name);
/**
* Remove all previously added appenders.
*/
virtual void removeAllAppenders();
/**
* Remove the appender passed as parameter from the list of appenders.
*/
virtual void removeAppender(SharedAppenderPtr appender);
/**
* Remove the appender with the name passed as parameter from the
* list of appenders.
*/
virtual void removeAppender(const log4cplus::tstring& name);
/**
* Call the <code>doAppend</code> method on all attached appenders.
*/
int appendLoopOnAppenders(const spi::InternalLoggingEvent& event) const;
protected:
// Types
typedef std::vector<SharedAppenderPtr> ListType;
// Data
/** Array of appenders. */
ListType appenderList;
private:
AppenderAttachableImpl(AppenderAttachableImpl const &);
AppenderAttachableImpl & operator = (AppenderAttachableImpl const &);
}; // end class AppenderAttachableImpl
} // end namespace helpers
} // end namespace log4cplus
#endif // LOG4CPLUS_HELPERS_APPENDER_ATTACHABLE_IMPL_HEADER_

View File

@@ -0,0 +1,107 @@
// -*- C++ -*-
// Copyright (C) 2013-2017, Vaclav Zeman. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modifica-
// tion, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
// DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef LOG4CPLUS_HELPERS_CONNECTORTHREAD_H
#define LOG4CPLUS_HELPERS_CONNECTORTHREAD_H
#include <log4cplus/config.hxx>
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#include <log4cplus/thread/syncprims.h>
#include <log4cplus/thread/threads.h>
#include <log4cplus/helpers/socket.h>
#if ! defined (LOG4CPLUS_SINGLE_THREADED)
namespace log4cplus { namespace helpers {
class LOG4CPLUS_EXPORT ConnectorThread;
//! Interface implemented by users of ConnectorThread.
class LOG4CPLUS_EXPORT IConnectorThreadClient
{
protected:
virtual ~IConnectorThreadClient ();
//! \return Mutex for synchronization between ConnectorThread and
//! its client object. This is usually SharedObject::access_mutex.
virtual thread::Mutex const & ctcGetAccessMutex () const = 0;
//! \return Socket variable in ConnectorThread client to maintain.
virtual helpers::Socket & ctcGetSocket () = 0;
//! \return ConnectorThread client's function returning connected
//! socket.
virtual helpers::Socket ctcConnect () = 0;
//! Sets connected flag to true in ConnectorThread's client.
virtual void ctcSetConnected () = 0;
friend class LOG4CPLUS_EXPORT ConnectorThread;
};
//! This class is used by SocketAppender and (remote) SysLogAppender
//! to provide asynchronous re-connection.
class LOG4CPLUS_EXPORT ConnectorThread
: public thread::AbstractThread
{
public:
//! \param client reference to ConnectorThread's client object
ConnectorThread (IConnectorThreadClient & client);
virtual ~ConnectorThread ();
virtual void run();
//! Call this function to terminate ConnectorThread. The function
//! sets `exit_flag` and then triggers `trigger_ev` to wake up the
//! ConnectorThread.
void terminate ();
//! This function triggers (`trigger_ev`) connection check and
//! attempt to re-connect a broken connection, when necessary.
void trigger ();
protected:
//! reference to ConnectorThread's client
IConnectorThreadClient & ctc;
//! This event is the re-connection trigger.
thread::ManualResetEvent trigger_ev;
//! When this variable set to true when ConnectorThread is signaled to
bool exit_flag;
};
} } // namespace log4cplus { namespace helpers {
#endif // ! defined (LOG4CPLUS_SINGLE_THREADED)
#endif // LOG4CPLUS_HELPERS_CONNECTORTHREAD_H

View File

@@ -0,0 +1,91 @@
// -*- C++ -*-
//
// Copyright (C) 2024, Vaclav Haisman. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modifica-
// tion, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
// DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef LOG4CPLUS_HELPERS_EVENTCOUNTER_H
#define LOG4CPLUS_HELPERS_EVENTCOUNTER_H
#include <log4cplus/config.hxx>
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#include <log4cplus/thread/syncprims.h>
#include <cstddef>
#include <atomic>
#include <chrono>
namespace log4cplus {
namespace helpers {
class LOG4CPLUS_EXPORT BaseEventCounter
{
public:
BaseEventCounter ();
virtual ~BaseEventCounter ();
virtual std::size_t record_event ();
protected:
std::atomic<std::size_t> event_count {0};
};
class LOG4CPLUS_EXPORT SteadyClockGate
: public BaseEventCounter
{
public:
using Clock = std::chrono::steady_clock;
using Duration = Clock::duration;
using TimePoint = std::chrono::time_point<Clock>;
struct LOG4CPLUS_EXPORT Info
{
~Info ();
std::size_t count;
Duration time_span;
};
SteadyClockGate (SteadyClockGate::Duration pause_duraiton);
virtual ~SteadyClockGate ();
bool latch_open (Info &);
private:
log4cplus::thread::SimpleMutex mtx;
Duration const pause_duration;
TimePoint timeout_point;
TimePoint prev_timeout_point;
};
} // namespace helpers
} // namespace log4cplus
#endif // LOG4CPLUS_HELPERS_EVENTCOUNTER_H

View File

@@ -0,0 +1,59 @@
// -*- C++ -*-
//
// Copyright (C) 2012-2017, Vaclav Zeman. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modifica-
// tion, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
// DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#if ! defined (LOG4CPLUS_HELPERS_FILEINFO_H)
#define LOG4CPLUS_HELPERS_FILEINFO_H
#include <log4cplus/config.hxx>
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#include <log4cplus/helpers/timehelper.h>
#ifdef LOG4CPLUS_HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
namespace log4cplus { namespace helpers {
//! FileInfo structure is OS independent abstraction of the
//! <code>stat()</code> function.
struct LOG4CPLUS_EXPORT FileInfo
{
helpers::Time mtime;
bool is_link;
off_t size;
};
//! OS independent abstraction of <code>stat()</code> function.
LOG4CPLUS_EXPORT int getFileInfo (FileInfo * fi, tstring const & name);
} } // namespace log4cplus { namespace helpers {
#endif // LOG4CPLUS_HELPERS_FILEINFO_H

View File

@@ -0,0 +1,69 @@
// -*- C++ -*-
//
// Copyright (C) 2012-2017, Vaclav Zeman. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modifica-
// tion, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
// DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#if ! defined (LOG4CPLUS_HELPERS_LOCKFILE_H)
#define LOG4CPLUS_HELPERS_LOCKFILE_H
#include <log4cplus/config.hxx>
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#include <log4cplus/tstring.h>
#include <log4cplus/thread/syncprims.h>
namespace log4cplus { namespace helpers {
class LOG4CPLUS_EXPORT LockFile
{
public:
LockFile (tstring const & lock_file, bool create_dirs = false);
~LockFile ();
void lock () const;
void unlock () const;
private:
void open (int) const;
void close () const;
struct Impl;
tstring lock_file_name;
Impl * data;
bool create_dirs;
};
typedef log4cplus::thread::SyncGuard<LockFile> LockFileGuard;
} } // namespace log4cplus { namespace helpers {
#endif // LOG4CPLUS_HELPERS_LOCKFILE_H

View File

@@ -0,0 +1,145 @@
// -*- C++ -*-
// Module: Log4CPLUS
// File: loglog.h
// Created: 6/2001
// Author: Tad E. Smith
//
//
// Copyright 2001-2017 Tad E. Smith
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/** @file */
#ifndef LOG4CPLUS_HELPERS_LOGLOG
#define LOG4CPLUS_HELPERS_LOGLOG
#include <log4cplus/config.hxx>
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#include <log4cplus/tstring.h>
#include <log4cplus/streams.h>
#include <log4cplus/thread/syncprims.h>
namespace log4cplus {
namespace helpers {
/**
* This class used to output log statements from within the log4cplus package.
*
* Log4cplus components cannot make log4cplus logging calls. However, it is
* sometimes useful for the user to learn about what log4cplus is
* doing. You can enable log4cplus internal logging by defining the
* <b>log4cplus.configDebug</b> variable.
*
* All log4cplus internal debug calls go to <code>cout</code>
* where as internal error messages are sent to
* <code>cerr</code>. All internal messages are prepended with
* the string "log4clus: ".
*/
class LOG4CPLUS_EXPORT LogLog
{
public:
//! Return type of getLogLog().
typedef LogLog * Ptr;
/**
* Returns a reference to the <code>LogLog</code> singleton.
*/
static Ptr getLogLog();
/**
* Allows to enable/disable log4cplus internal logging.
*/
void setInternalDebugging(bool enabled);
/**
* In quite mode no LogLog generates strictly no output, not even
* for errors.
*
* @param quietMode A true for not
*/
void setQuietMode(bool quietMode);
/**
* This method is used to output log4cplus internal debug
* statements. Output goes to <code>std::cout</code>.
*/
void debug(const log4cplus::tstring& msg) const;
void debug(tchar const * msg) const;
/**
* This method is used to output log4cplus internal error
* statements. There is no way to disable error
* statements. Output goes to
* <code>std::cerr</code>. Optionally, this method can
* throw std::runtime_error exception too.
*/
void error(const log4cplus::tstring& msg, bool throw_flag = false) const;
void error(tchar const * msg, bool throw_flag = false) const;
/**
* This method is used to output log4cplus internal warning
* statements. There is no way to disable warning statements.
* Output goes to <code>std::cerr</code>.
*/
void warn(const log4cplus::tstring& msg) const;
void warn(tchar const * msg) const;
// Public ctor and dtor to be used only by internal::DefaultContext.
LogLog();
virtual ~LogLog();
private:
enum TriState
{
TriUndef = -1,
TriFalse,
TriTrue
};
template <typename StringType>
LOG4CPLUS_PRIVATE
void logging_worker (tostream & os,
bool (LogLog:: * cond) () const, tchar const *,
StringType const &, bool throw_flag = false) const;
LOG4CPLUS_PRIVATE static void set_tristate_from_env (TriState *,
tchar const * envvar);
LOG4CPLUS_PRIVATE bool get_quiet_mode () const;
LOG4CPLUS_PRIVATE bool get_not_quiet_mode () const;
LOG4CPLUS_PRIVATE bool get_debug_mode () const;
// Data
mutable TriState debugEnabled;
mutable TriState quietMode;
thread::Mutex mutex;
LOG4CPLUS_PRIVATE LogLog(const LogLog&);
LOG4CPLUS_PRIVATE LogLog & operator = (LogLog const &);
};
LOG4CPLUS_EXPORT LogLog & getLogLog ();
} // end namespace helpers
} // end namespace log4cplus
#endif // LOG4CPLUS_HELPERS_LOGLOG

View File

@@ -0,0 +1,210 @@
// -*- C++ -*-
// Module: Log4CPLUS
// File: pointer.h
// Created: 6/2001
// Author: Tad E. Smith
//
//
// Copyright 2001-2017 Tad E. Smith
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Note: Some of this code uses ideas from "More Effective C++" by Scott
// Myers, Addison Wesley Longmain, Inc., (c) 1996, Chapter 29, pp. 183-213
//
/** @file */
#ifndef LOG4CPLUS_HELPERS_POINTERS_HEADER_
#define LOG4CPLUS_HELPERS_POINTERS_HEADER_
#include <log4cplus/config.hxx>
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#include <log4cplus/thread/syncprims.h>
#include <algorithm>
#include <cassert>
#if ! defined (LOG4CPLUS_SINGLE_THREADED)
#include <atomic>
#endif
namespace log4cplus {
namespace helpers {
/******************************************************************************
* Class SharedObject (from pp. 204-205) *
******************************************************************************/
class LOG4CPLUS_EXPORT SharedObject
{
public:
void addReference() const LOG4CPLUS_NOEXCEPT;
void removeReference() const;
protected:
// Ctor
SharedObject()
: access_mutex()
, count__(0)
{ }
SharedObject(const SharedObject&)
: access_mutex()
, count__(0)
{ }
SharedObject(SharedObject &&)
: access_mutex()
, count__(0)
{ }
// Dtor
virtual ~SharedObject();
// Operators
SharedObject& operator=(const SharedObject&) LOG4CPLUS_NOEXCEPT { return *this; }
SharedObject& operator=(SharedObject &&) LOG4CPLUS_NOEXCEPT { return *this; }
public:
thread::Mutex access_mutex;
private:
#if defined (LOG4CPLUS_SINGLE_THREADED)
typedef unsigned count_type;
#else
typedef std::atomic<unsigned> count_type;
#endif
mutable count_type count__;
};
/******************************************************************************
* Template Class SharedObjectPtr (from pp. 203, 206) *
******************************************************************************/
template<class T>
class SharedObjectPtr
{
public:
// Ctor
explicit
SharedObjectPtr(T* realPtr = 0) LOG4CPLUS_NOEXCEPT
: pointee(realPtr)
{
addref ();
}
SharedObjectPtr(const SharedObjectPtr& rhs) LOG4CPLUS_NOEXCEPT
: pointee(rhs.pointee)
{
addref ();
}
SharedObjectPtr(SharedObjectPtr && rhs) LOG4CPLUS_NOEXCEPT
: pointee (std::move (rhs.pointee))
{
rhs.pointee = 0;
}
SharedObjectPtr & operator = (SharedObjectPtr && rhs) LOG4CPLUS_NOEXCEPT
{
rhs.swap (*this);
return *this;
}
// Dtor
~SharedObjectPtr()
{
if (pointee)
pointee->removeReference();
}
// Operators
bool operator==(const SharedObjectPtr& rhs) const
{ return (pointee == rhs.pointee); }
bool operator!=(const SharedObjectPtr& rhs) const
{ return (pointee != rhs.pointee); }
bool operator==(const T* rhs) const { return (pointee == rhs); }
bool operator!=(const T* rhs) const { return (pointee != rhs); }
T* operator->() const {assert (pointee); return pointee; }
T& operator*() const {assert (pointee); return *pointee; }
SharedObjectPtr& operator=(const SharedObjectPtr& rhs)
{
return this->operator = (rhs.pointee);
}
SharedObjectPtr& operator=(T* rhs)
{
SharedObjectPtr<T> (rhs).swap (*this);
return *this;
}
// Methods
T* get() const { return pointee; }
void swap (SharedObjectPtr & other) LOG4CPLUS_NOEXCEPT
{
std::swap (pointee, other.pointee);
}
typedef T * (SharedObjectPtr:: * unspec_bool_type) () const;
operator unspec_bool_type () const
{
return pointee ? &SharedObjectPtr::get : 0;
}
bool operator ! () const
{
return ! pointee;
}
private:
// Methods
void addref() const LOG4CPLUS_NOEXCEPT
{
if (pointee)
pointee->addReference();
}
// Data
T* pointee;
};
//! Boost `intrusive_ptr` helpers.
//! @{
inline
void
intrusive_ptr_add_ref (SharedObject const * so)
{
so->addReference();
}
inline
void
intrusive_ptr_release (SharedObject const * so)
{
so->removeReference();
}
//! @}
} // end namespace helpers
} // end namespace log4cplus
#endif // LOG4CPLUS_HELPERS_POINTERS_HEADER_

View File

@@ -0,0 +1,172 @@
// -*- C++ -*-
// Module: Log4CPLUS
// File: property.h
// Created: 2/2002
// Author: Tad E. Smith
//
//
// Copyright 2002-2017 Tad E. Smith
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/** @file */
#ifndef LOG4CPLUS_HELPERS_PROPERTY_HEADER_
#define LOG4CPLUS_HELPERS_PROPERTY_HEADER_
#include <log4cplus/config.hxx>
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#include <log4cplus/streams.h>
#include <log4cplus/tstring.h>
#include <map>
#include <vector>
namespace log4cplus {
namespace helpers {
//! \sa log4cplus::PropertyConfigurator
class LOG4CPLUS_EXPORT Properties {
public:
enum PFlags
{
// These encoding related options occupy 2 bits of the flags
// and are mutually exclusive. These flags are synchronized
// with PCFlags in PropertyConfigurator.
fEncodingShift = 3
, fEncodingMask = 0x3
, fUnspecEncoding = (0 << fEncodingShift)
#if defined (LOG4CPLUS_HAVE_CODECVT_UTF8_FACET) && defined (UNICODE)
, fUTF8 = (1 << fEncodingShift)
#endif
#if (defined (LOG4CPLUS_HAVE_CODECVT_UTF16_FACET) || defined (_WIN32)) \
&& defined (UNICODE)
, fUTF16 = (2 << fEncodingShift)
#endif
#if defined (LOG4CPLUS_HAVE_CODECVT_UTF32_FACET) && defined (UNICODE)
, fUTF32 = (3 << fEncodingShift)
#endif
, fThrow = (1 << 5)
};
Properties();
explicit Properties(log4cplus::tistream& input);
explicit Properties(const log4cplus::tstring& inputFile, unsigned flags = 0);
virtual ~Properties();
// constants
static const tchar PROPERTIES_COMMENT_CHAR;
// methods
/**
* Tests to see if <code>key</code> can be found in this map.
*/
bool exists(const log4cplus::tstring& key) const;
bool exists(tchar const * key) const;
/**
* Returns the number of entries in this map.
*/
std::size_t size() const
{
return data.size();
}
/**
* Searches for the property with the specified key in this property
* list. If the key is not found in this property list, the default
* property list, and its defaults, recursively, are then checked.
* The method returns <code>null</code> if the property is not found.
*/
log4cplus::tstring const & getProperty(const log4cplus::tstring& key) const;
log4cplus::tstring const & getProperty(tchar const * key) const;
/**
* Searches for the property with the specified key in this property
* list. If the key is not found in this property list, the default
* property list, and its defaults, recursively, are then checked.
* The method returns the default value argument if the property is
* not found.
*/
log4cplus::tstring getProperty(const log4cplus::tstring& key,
const log4cplus::tstring& defaultVal) const;
/**
* Returns all the keys in this property list.
*/
std::vector<log4cplus::tstring> propertyNames() const;
/**
* Inserts <code>value</code> into this map indexed by <code>key</code>.
*/
void setProperty(const log4cplus::tstring& key, const log4cplus::tstring& value);
/**
* Removed the property index by <code>key</code> from this map.
*/
bool removeProperty(const log4cplus::tstring& key);
/**
* Returns a subset of the "properties" whose keys start with
* "prefix". The returned "properties" have "prefix" trimmed from
* their keys.
*/
Properties getPropertySubset(const log4cplus::tstring& prefix) const;
bool getInt (int & val, log4cplus::tstring const & key) const;
bool getUInt (unsigned & val, log4cplus::tstring const & key) const;
bool getLong (long & val, log4cplus::tstring const & key) const;
bool getULong (unsigned long & val, log4cplus::tstring const & key) const;
bool getBool (bool & val, log4cplus::tstring const & key) const;
bool getString (log4cplus::tstring & val, log4cplus::tstring const & key) const;
protected:
// Types
typedef std::map<log4cplus::tstring, log4cplus::tstring> StringMap;
// Methods
void init(log4cplus::tistream& input);
// Data
StringMap data;
unsigned flags;
private:
template <typename StringType>
log4cplus::tstring const & get_property_worker (
StringType const & key) const;
template <typename ValType>
bool get_type_val_worker (ValType & val,
log4cplus::tstring const & key) const;
};
class LogLog;
bool
substVars (tstring & dest, const tstring & val,
Properties const & props, LogLog& loglog,
unsigned flags);
} // end namespace helpers
}
#endif // LOG4CPLUS_HELPERS_PROPERTY_HEADER_

View File

@@ -0,0 +1,158 @@
// -*- C++ -*-
// Copyright (C) 2009-2017, Vaclav Haisman. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modifica-
// tion, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
// DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef LOG4CPLUS_HELPERS_QUEUE_H
#define LOG4CPLUS_HELPERS_QUEUE_H
#include <log4cplus/config.hxx>
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#if ! defined (LOG4CPLUS_SINGLE_THREADED)
#include <deque>
#include <log4cplus/spi/loggingevent.h>
#include <log4cplus/thread/threads.h>
#include <log4cplus/thread/syncprims.h>
namespace log4cplus { namespace thread {
//! Single consumer, multiple producers queue.
class LOG4CPLUS_EXPORT Queue
: public virtual helpers::SharedObject
{
public:
//! Type of the state flags field.
typedef unsigned flags_type;
//! Queue storage type.
typedef std::deque<spi::InternalLoggingEvent> queue_storage_type;
explicit Queue (unsigned len = 100);
virtual ~Queue ();
// Producers' methods.
//! Puts event <code>ev</code> into queue, sets QUEUE flag and
//! sets internal event object into signaled state. If the EXIT
//! flags is already set upon entering the function, nothing is
//! inserted into the queue. The function can block on internal
//! semaphore if the queue has reached maximal allowed
//! length. Calling thread is unblocked either by consumer thread
//! removing item from queue or by any other thread calling
//! signal_exit().
//!
//! \param ev spi::InternalLoggingEvent to be put into the queue.
//! \return Flags.
flags_type put_event (spi::InternalLoggingEvent const & ev);
//! Sets EXIT flag and DRAIN flag and sets internal event object
//! into signaled state.
//! \param drain If true, DRAIN flag will be set, otherwise unset.
//! \return Flags, ERROR_BIT can be set upon error.
flags_type signal_exit (bool drain = true);
// Consumer's methods.
//! The get_events() function is used by queue's consumer. It
//! fills <code>buf</code> argument and sets EVENT flag in return
//! value. If EXIT flag is already set in flags member upon
//! entering the function then depending on DRAIN flag it either
//! fills <code>buf</code> argument or does not fill the argument,
//! if the queue is non-empty. The function blocks by waiting for
//! internal event object to be signaled if the queue is empty,
//! unless EXIT flag is set. The calling thread is unblocked when
//! items are added into the queue or when exit is signaled using
//! the signal_exit() function.
//!
//!
//! Upon error, return value has one of the error flags set.
//!
//! \param buf Pointer to storage of spi::InternalLoggingEvent
//! instances to be filled from queue.
//! \return Flags.
flags_type get_events (queue_storage_type * buf);
//! Possible state flags.
enum Flags
{
//! EVENT flag is set in return value of get_event() call if
//! the <code>ev</code> argument is filled with event from the queue.
EVENT = 0x0001,
//! QUEUE flag is set by producers when they put item into the
//! queue.
QUEUE = 0x0002,
//! EXIT flag is set by signal_exit() call, signaling that the
//! queue worker thread should end itself.
EXIT = 0x0004,
//! When DRAIN flag is set together with EXIT flag, the queue
//! worker thread will first drain the queue before exiting.
DRAIN = 0x0008,
//! ERROR_BIT signals error.
ERROR_BIT = 0x0010,
//! ERROR_AFTER signals error that has occurred after queue has
//! already been touched.
ERROR_AFTER = 0x0020
};
protected:
//! Queue storage.
queue_storage_type queue;
//! Mutex protecting queue and flags.
Mutex mutex;
//! Event on which consumer can wait if it finds queue empty.
ManualResetEvent ev_consumer;
//! Semaphore that limits the queue length.
Semaphore sem;
//! State flags.
flags_type flags;
private:
Queue (Queue const &);
Queue & operator = (Queue const &);
};
typedef helpers::SharedObjectPtr<Queue> QueuePtr;
} } // namespace log4cplus { namespace thread {
#endif // LOG4CPLUS_SINGLE_THREADED
#endif // LOG4CPLUS_HELPERS_QUEUE_H

View File

@@ -0,0 +1,62 @@
// -*- C++ -*-
// Copyright (C) 2010-2017, Vaclav Zeman. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modifica-
// tion, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
// DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef LOG4CPLUS_HELPERS_SNPRINTF_H
#define LOG4CPLUS_HELPERS_SNPRINTF_H
#include <log4cplus/config.hxx>
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#include <log4cplus/tchar.h>
#include <cstdarg>
#include <vector>
namespace log4cplus { namespace helpers {
class LOG4CPLUS_EXPORT snprintf_buf
{
public:
snprintf_buf ();
tchar const * print (tchar const * fmt, ...)
LOG4CPLUS_FORMAT_ATTRIBUTE (__printf__, 2, 3);
int print_va_list (tchar const * & str, tchar const * fmt, std::va_list)
LOG4CPLUS_FORMAT_ATTRIBUTE (__printf__, 3, 0);
private:
std::vector<tchar> buf;
};
} } // namespace log4cplus { namespace helpers
#endif // LOG4CPLUS_HELPERS_SNPRINTF_H

View File

@@ -0,0 +1,163 @@
// -*- C++ -*-
// Module: Log4CPLUS
// File: socket.h
// Created: 4/2003
// Author: Tad E. Smith
//
//
// Copyright 2003-2017 Tad E. Smith
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/** @file */
#ifndef LOG4CPLUS_HELPERS_SOCKET_HEADER_
#define LOG4CPLUS_HELPERS_SOCKET_HEADER_
#include <log4cplus/config.hxx>
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#include <array>
#include <log4cplus/tstring.h>
#include <log4cplus/helpers/socketbuffer.h>
namespace log4cplus {
namespace helpers {
enum SocketState { ok,
not_opened,
bad_address,
connection_failed,
broken_pipe,
invalid_access_mode,
message_truncated,
accept_interrupted
};
typedef std::ptrdiff_t SOCKET_TYPE;
extern LOG4CPLUS_EXPORT SOCKET_TYPE const INVALID_SOCKET_VALUE;
class LOG4CPLUS_EXPORT AbstractSocket {
public:
AbstractSocket();
AbstractSocket(SOCKET_TYPE sock, SocketState state, int err);
AbstractSocket(AbstractSocket const &) = delete;
AbstractSocket(AbstractSocket &&) LOG4CPLUS_NOEXCEPT;
virtual ~AbstractSocket() = 0;
/// Close socket
virtual void close();
virtual bool isOpen() const;
virtual void shutdown();
AbstractSocket & operator = (AbstractSocket && rhs) LOG4CPLUS_NOEXCEPT;
void swap (AbstractSocket &);
protected:
SOCKET_TYPE sock;
SocketState state;
int err;
};
/**
* This class implements client sockets (also called just "sockets").
* A socket is an endpoint for communication between two machines.
*/
class LOG4CPLUS_EXPORT Socket : public AbstractSocket {
public:
// ctor and dtor
Socket();
Socket(SOCKET_TYPE sock, SocketState state, int err);
Socket(const tstring& address, unsigned short port,
bool udp = false, bool ipv6 = false);
Socket(Socket &&) LOG4CPLUS_NOEXCEPT;
virtual ~Socket();
Socket & operator = (Socket &&) LOG4CPLUS_NOEXCEPT;
// methods
virtual bool read(SocketBuffer& buffer);
virtual bool write(const SocketBuffer& buffer);
virtual bool write(const std::string & buffer);
virtual bool write(std::size_t bufferCount,
SocketBuffer const * const * buffers);
template <typename... Args>
static bool write(Socket & socket, Args &&... args)
{
SocketBuffer const * const buffers[sizeof... (args)] {
(&args)... };
return socket.write (sizeof... (args), buffers);
}
};
/**
* This class implements server sockets. A server socket waits for
* requests to come in over the network. It performs some operation
* based on that request, and then possibly returns a result to the
* requester.
*/
class LOG4CPLUS_EXPORT ServerSocket : public AbstractSocket {
public:
ServerSocket(unsigned short port, bool udp = false,
bool ipv6 = false, tstring const & host = tstring ());
ServerSocket(ServerSocket &&) LOG4CPLUS_NOEXCEPT;
virtual ~ServerSocket();
ServerSocket & operator = (ServerSocket &&) LOG4CPLUS_NOEXCEPT;
Socket accept();
void interruptAccept ();
void swap (ServerSocket &);
protected:
std::array<std::ptrdiff_t, 2> interruptHandles;
};
LOG4CPLUS_EXPORT SOCKET_TYPE openSocket(unsigned short port, bool udp,
bool ipv6, SocketState& state);
LOG4CPLUS_EXPORT SOCKET_TYPE openSocket(tstring const & host,
unsigned short port, bool udp, bool ipv6, SocketState& state);
LOG4CPLUS_EXPORT SOCKET_TYPE connectSocket(const log4cplus::tstring& hostn,
unsigned short port, bool udp, bool ipv6, SocketState& state);
LOG4CPLUS_EXPORT SOCKET_TYPE acceptSocket(SOCKET_TYPE sock, SocketState& state);
LOG4CPLUS_EXPORT int closeSocket(SOCKET_TYPE sock);
LOG4CPLUS_EXPORT int shutdownSocket(SOCKET_TYPE sock);
LOG4CPLUS_EXPORT long read(SOCKET_TYPE sock, SocketBuffer& buffer);
LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock,
const SocketBuffer& buffer);
LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock, std::size_t bufferCount,
SocketBuffer const * const * buffers);
LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock,
const std::string & buffer);
LOG4CPLUS_EXPORT tstring getHostname (bool fqdn);
LOG4CPLUS_EXPORT int setTCPNoDelay (SOCKET_TYPE, bool);
} // end namespace helpers
} // end namespace log4cplus
#endif // LOG4CPLUS_HELPERS_SOCKET_HEADER_

View File

@@ -0,0 +1,79 @@
// -*- C++ -*-
// Module: Log4CPLUS
// File: socketbuffer.h
// Created: 5/2003
// Author: Tad E. Smith
//
//
// Copyright 2003-2017 Tad E. Smith
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/** @file */
#ifndef LOG4CPLUS_HELPERS_SOCKET_BUFFER_HEADER_
#define LOG4CPLUS_HELPERS_SOCKET_BUFFER_HEADER_
#include <log4cplus/config.hxx>
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#include <log4cplus/tstring.h>
namespace log4cplus {
namespace helpers {
/**
*
*/
class LOG4CPLUS_EXPORT SocketBuffer
{
public:
explicit SocketBuffer(std::size_t max);
virtual ~SocketBuffer();
char *getBuffer() const { return buffer; }
std::size_t getMaxSize() const { return maxsize; }
std::size_t getSize() const { return size; }
void setSize(std::size_t s) { size = s; }
std::size_t getPos() const { return pos; }
unsigned char readByte();
unsigned short readShort();
unsigned int readInt();
tstring readString(unsigned char sizeOfChar);
void appendByte(unsigned char val);
void appendShort(unsigned short val);
void appendInt(unsigned int val);
void appendString(const tstring& str);
void appendBuffer(const SocketBuffer& buffer);
private:
// Data
std::size_t maxsize;
std::size_t size;
std::size_t pos;
char *buffer;
SocketBuffer(SocketBuffer const & rhs);
SocketBuffer& operator= (SocketBuffer const& rhs);
};
} // end namespace helpers
} // end namespace log4cplus
#endif // LOG4CPLUS_HELPERS_SOCKET_HEADER_

View File

@@ -0,0 +1,271 @@
// -*- C++ -*-
// Module: Log4CPLUS
// File: stringhelper.h
// Created: 3/2003
// Author: Tad E. Smith
//
//
// Copyright 2003-2017 Tad E. Smith
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/** @file */
#ifndef LOG4CPLUS_HELPERS_STRINGHELPER_HEADER_
#define LOG4CPLUS_HELPERS_STRINGHELPER_HEADER_
#include <log4cplus/config.hxx>
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#include <log4cplus/tstring.h>
#include <algorithm>
#include <limits>
namespace log4cplus {
namespace helpers {
/**
* Returns <code>s</code> in upper case.
*/
LOG4CPLUS_EXPORT log4cplus::tstring toUpper(const log4cplus::tstring& s);
LOG4CPLUS_EXPORT tchar toUpper(tchar);
/**
* Returns <code>s</code> in lower case.
*/
LOG4CPLUS_EXPORT log4cplus::tstring toLower(const log4cplus::tstring& s);
LOG4CPLUS_EXPORT tchar toLower(tchar);
/**
* Tokenize <code>s</code> using <code>c</code> as the delimiter and
* put the resulting tokens in <code>_result</code>. If
* <code>collapseTokens</code> is false, multiple adjacent delimiters
* will result in zero length tokens.
*
* <b>Example:</b>
* <pre>
* string s = // Set string with '.' as delimiters
* list<log4cplus::tstring> tokens;
* tokenize(s, '.', back_insert_iterator<list<string> >(tokens));
* </pre>
*/
template <class StringType, class OutputIter>
inline
void
tokenize(const StringType& s, typename StringType::value_type c,
OutputIter result, bool collapseTokens = true)
{
typedef typename StringType::size_type size_type;
size_type const slen = s.length();
size_type first = 0;
size_type i = 0;
for (i=0; i < slen; ++i)
{
if (s[i] == c)
{
*result = StringType (s, first, i - first);
++result;
if (collapseTokens)
while (i+1 < slen && s[i+1] == c)
++i;
first = i + 1;
}
}
if (first != i)
*result = StringType (s, first, i - first);
else if (! collapseTokens && first == i)
*result = StringType ();
}
template <typename intType, typename stringType, bool isSigned>
struct ConvertIntegerToStringHelper;
template <typename intType, typename charType>
struct ConvertIntegerToStringHelper<intType, charType, true>
{
static inline
void
step1 (charType * & it, intType & value)
{
// The sign of the result of the modulo operator is
// implementation defined. That's why we work with
// positive counterpart instead. Also, in twos
// complement arithmetic the smallest negative number
// does not have positive counterpart; the range is
// asymetric. That's why we handle the case of value
// == min() specially here.
if (LOG4CPLUS_UNLIKELY (
value == (std::numeric_limits<intType>::min) ()))
{
intType const r = value / 10;
intType const a = (-r) * 10;
intType const mod = -(a + value);
value = -r;
*(it - 1)
= static_cast<charType>(LOG4CPLUS_TEXT('0') + mod);
--it;
}
else
value = -value;
}
static
bool
is_negative (intType val)
{
return val < 0;
}
};
template <typename intType, typename charType>
struct ConvertIntegerToStringHelper<intType, charType, false>
{
static inline
void
step1 (charType * &, intType &)
{
// This will never be called for unsigned types.
}
static
bool
is_negative (intType)
{
return false;
}
};
template <class stringType, class intType>
inline
void
convertIntegerToString (stringType & str, intType value)
{
typedef std::numeric_limits<intType> intTypeLimits;
typedef typename stringType::value_type charType;
typedef ConvertIntegerToStringHelper<intType, charType,
intTypeLimits::is_signed> HelperType;
charType buffer[intTypeLimits::digits10 + 2];
const std::size_t buffer_size
= sizeof (buffer) / sizeof (charType);
charType * it = &buffer[buffer_size];
charType const * const buf_end = &buffer[buffer_size];
if (LOG4CPLUS_UNLIKELY (value == 0))
{
--it;
*it = LOG4CPLUS_TEXT('0');
}
else
{
bool const negative = HelperType::is_negative (value);
if (negative)
HelperType::step1 (it, value);
for (; value != 0; --it)
{
intType mod = value % 10;
value = value / 10;
*(it - 1) = static_cast<charType>(LOG4CPLUS_TEXT('0')
+ mod);
}
if (negative)
{
--it;
*it = LOG4CPLUS_TEXT('-');
}
}
str.assign (static_cast<charType const *>(it), buf_end);
}
template<class intType>
inline
tstring
convertIntegerToString (intType value)
{
tstring result;
convertIntegerToString (result, value);
return result;
}
template<class intType>
inline
std::string
convertIntegerToNarrowString (intType value)
{
std::string result;
convertIntegerToString (result, value);
return result;
}
//! Join a list of items into a string.
template <typename Iterator, typename Separator>
inline
void
join_worker (tstring & result, Iterator & start, Iterator & last,
Separator const & sep)
{
if (start != last)
result = *start++;
for (; start != last; ++start)
{
result += sep;
result += *start;
}
}
//! Join a list of items into a string.
template <typename Iterator>
inline
void
join (tstring & result, Iterator start, Iterator last,
tstring const & sep)
{
join_worker (result, start, last, sep);
}
//! Join a list of items into a string.
template <typename Iterator>
inline
void
join (tstring & result, Iterator start, Iterator last,
tstring::value_type sep)
{
join_worker (result, start, last, sep);
}
} // namespace helpers
} // namespace log4cplus
#endif // LOG4CPLUS_HELPERS_STRINGHELPER_HEADER_

View File

@@ -0,0 +1,58 @@
// -*- C++ -*-
// Module: Log4CPLUS
// File: thread-config.h
// Created: 4/2003
// Author: Tad E. Smith
//
//
// Copyright 2003-2017 Tad E. Smith
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/** @file */
#ifndef LOG4CPLUS_HELPERS_THREAD_CONFIG_HEADER_
#define LOG4CPLUS_HELPERS_THREAD_CONFIG_HEADER_
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#if defined (LOG4CPLUS_USE_PTHREADS)
# if defined (__APPLE__)
# define LOG4CPLUS_USE_NAMED_POSIX_SEMAPHORE
# endif
#elif defined(LOG4CPLUS_USE_WIN32_THREADS)
# define LOG4CPLUS_USE_SRW_LOCK
//# define LOG4CPLUS_POOR_MANS_SHAREDMUTEX
# undef LOG4CPLUS_HAVE_TLS_SUPPORT
# undef LOG4CPLUS_THREAD_LOCAL_VAR
# if defined (_MSC_VER)
// The __declspec(thread) functionality is not compatible with LoadLibrary().
// For more information why see and "Windows and TLS" note in README.
// <http://msdn.microsoft.com/en-us/library/2s9wt68x(v=vs.100).aspx>.
# define LOG4CPLUS_HAVE_TLS_SUPPORT 1
# define LOG4CPLUS_THREAD_LOCAL_VAR __declspec(thread)
# endif
#elif defined(LOG4CPLUS_SINGLE_THREADED)
# undef LOG4CPLUS_HAVE_TLS_SUPPORT
# undef LOG4CPLUS_THREAD_LOCAL_VAR
#else
# error "You Must define a Threading model"
#endif
#endif // LOG4CPLUS_HELPERS_THREAD_CONFIG_HEADER_

View File

@@ -0,0 +1,169 @@
// -*- C++ -*-
// Module: Log4CPLUS
// File: timehelper.h
// Created: 6/2003
// Author: Tad E. Smith
//
//
// Copyright 2003-2017 Tad E. Smith
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/** @file */
#ifndef LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_
#define LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_
#include <log4cplus/config.hxx>
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
#pragma once
#endif
#include <log4cplus/tstring.h>
#if defined (LOG4CPLUS_HAVE_TIME_H)
#include <time.h>
#endif
#include <ctime>
#include <chrono>
namespace log4cplus {
namespace helpers {
using std::time_t;
using std::tm;
namespace chrono = std::chrono;
typedef chrono::system_clock Clock;
typedef chrono::duration<long long, std::micro> Duration;
typedef chrono::time_point<Clock, Duration> Time;
template <typename FromDuration>
inline
Time
time_cast (chrono::time_point<Clock, FromDuration> const & tp)
{
return chrono::time_point_cast<Duration, Clock> (tp);
}
inline
Time
now ()
{
return time_cast (Clock::now ());
}
inline
Time
from_time_t (time_t t_time)
{
return time_cast (Clock::from_time_t (t_time));
}
inline
time_t
to_time_t (Time const & the_time)
{
// This is based on <http://stackoverflow.com/a/17395137/341065>. It is
// possible that to_time_t() returns rounded time and we want truncation.
time_t time = Clock::to_time_t (the_time);
auto const rounded_time = from_time_t (time);
if (rounded_time > the_time)
--time;
return time;
}
LOG4CPLUS_EXPORT Time from_struct_tm (tm * t);
inline
Time
truncate_fractions (Time const & the_time)
{
return from_time_t (to_time_t (the_time));
}
inline
long
microseconds_part (Time const & the_time)
{
static_assert ((std::ratio_equal<Duration::period, std::micro>::value),
"microseconds");
// This is based on <http://stackoverflow.com/a/17395137/341065>
return static_cast<long>(
(the_time - from_time_t (to_time_t (the_time))).count ());
}
inline
Time
time_from_parts (time_t tv_sec, long tv_usec)
{
return from_time_t (tv_sec) + chrono::microseconds (tv_usec);
}
/**
* Populates <code>tm</code> using the <code>gmtime()</code>
* function.
*/
LOG4CPLUS_EXPORT
void gmTime (tm* t, Time const &);
/**
* Populates <code>tm</code> using the <code>localtime()</code>
* function.
*/
LOG4CPLUS_EXPORT
void localTime (tm* t, Time const &);
/**
* Returns a string with a "formatted time" specified by
* <code>fmt</code>. It used the <code>strftime()</code>
* function to do this.
*
* Look at your platform's <code>strftime()</code> documentation
* for the formatting options available.
*
* The following additional options are provided:<br>
* <code>%q</code> - 3 character field that provides milliseconds
* <code>%Q</code> - 7 character field that provides fractional
* milliseconds.
*/
LOG4CPLUS_EXPORT
log4cplus::tstring getFormattedTime (log4cplus::tstring const & fmt,
Time const & the_time, bool use_gmtime = false);
} // namespace helpers
} // namespace log4cplus
#endif // LOG4CPLUS_HELPERS_TIME_HELPER_HEADER_