Coverage Report

Created: 2025-07-11 07:00

/src/logging-log4cxx/src/main/cpp/datepatternconverter.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
#include <log4cxx/logstring.h>
19
#include <log4cxx/pattern/datepatternconverter.h>
20
#include <log4cxx/spi/loggingevent.h>
21
#include <log4cxx/spi/location/locationinfo.h>
22
#include <log4cxx/helpers/absolutetimedateformat.h>
23
#include <log4cxx/helpers/datetimedateformat.h>
24
#include <log4cxx/helpers/iso8601dateformat.h>
25
#include <log4cxx/helpers/strftimedateformat.h>
26
#include <log4cxx/helpers/stringhelper.h>
27
#include <log4cxx/helpers/exception.h>
28
#include <log4cxx/helpers/loglog.h>
29
#include <log4cxx/helpers/date.h>
30
#include <log4cxx/private/patternconverter_priv.h>
31
32
using namespace LOG4CXX_NS;
33
using namespace LOG4CXX_NS::pattern;
34
using namespace LOG4CXX_NS::spi;
35
using namespace LOG4CXX_NS::helpers;
36
37
struct DatePatternConverter::DatePatternConverterPrivate : public PatternConverterPrivate
38
{
39
  DatePatternConverterPrivate( const LogString& name, const LogString& style, DateFormatPtr _df ):
40
0
    PatternConverterPrivate(name, style),
41
0
    df(_df) {}
42
  /**
43
   * Date format.
44
   */
45
  LOG4CXX_NS::helpers::DateFormatPtr df;
46
};
47
48
0
#define priv static_cast<DatePatternConverterPrivate*>(m_priv.get())
49
50
IMPLEMENT_LOG4CXX_OBJECT(DatePatternConverter)
51
52
DatePatternConverter::DatePatternConverter(
53
  const std::vector<LogString>& options) :
54
0
  LoggingEventPatternConverter (std::make_unique<DatePatternConverterPrivate>(LOG4CXX_STR("Class Name"),
55
0
      LOG4CXX_STR("class name"), getDateFormat(options)))
56
0
{
57
0
}
Unexecuted instantiation: log4cxx::pattern::DatePatternConverter::DatePatternConverter(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&)
Unexecuted instantiation: log4cxx::pattern::DatePatternConverter::DatePatternConverter(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&)
58
59
0
DatePatternConverter::~DatePatternConverter() {}
60
61
DateFormatPtr DatePatternConverter::getDateFormat(const OptionsList& options)
62
0
{
63
0
  DateFormatPtr df;
64
0
  int maximumCacheValidity = 1000000;
65
66
0
  if (options.size() == 0)
67
0
  {
68
0
    df = std::make_shared<ISO8601DateFormat>();
69
0
  }
70
0
  else
71
0
  {
72
0
    LogString dateFormatStr(options[0]);
73
74
0
    if (dateFormatStr.empty() ||
75
0
      StringHelper::equalsIgnoreCase(dateFormatStr,
76
0
        LOG4CXX_STR("ISO8601"), LOG4CXX_STR("iso8601")))
77
0
    {
78
0
      df = std::make_shared<ISO8601DateFormat>();
79
0
    }
80
0
    else if (StringHelper::equalsIgnoreCase(dateFormatStr,
81
0
        LOG4CXX_STR("ABSOLUTE"), LOG4CXX_STR("absolute")))
82
0
    {
83
0
      df = std::make_shared<AbsoluteTimeDateFormat>();
84
0
    }
85
0
    else if (StringHelper::equalsIgnoreCase(dateFormatStr,
86
0
        LOG4CXX_STR("DATE"), LOG4CXX_STR("date")))
87
0
    {
88
0
      df = std::make_shared<DateTimeDateFormat>();
89
0
    }
90
0
    else
91
0
    {
92
0
      if (dateFormatStr.find(0x25 /*'%'*/) == std::string::npos)
93
0
      {
94
0
        try
95
0
        {
96
0
          df = std::make_shared<SimpleDateFormat>(dateFormatStr);
97
0
          maximumCacheValidity =
98
0
            CachedDateFormat::getMaximumCacheValidity(dateFormatStr);
99
0
        }
100
0
        catch (std::exception& e)
101
0
        {
102
0
          df = std::make_shared<ISO8601DateFormat>();
103
0
          LogLog::warn(((LogString)
104
0
              LOG4CXX_STR("Could not instantiate SimpleDateFormat with pattern "))
105
0
            + dateFormatStr, e);
106
0
        }
107
0
      }
108
0
      else
109
0
      {
110
0
        df = std::make_shared<StrftimeDateFormat>(dateFormatStr);
111
0
      }
112
0
    }
113
114
0
    if (options.size() >= 2)
115
0
    {
116
0
      TimeZonePtr tz;
117
0
      try
118
0
      {
119
0
        tz = TimeZone::getTimeZone(options[1]);
120
0
      }
121
0
      catch (std::exception& e)
122
0
      {
123
0
        LogLog::warn(LOG4CXX_STR("Invalid time zone: ") + options[1], e);
124
0
      }
125
126
0
      if (tz)
127
0
      {
128
0
        df->setTimeZone(tz);
129
0
      }
130
0
    }
131
0
  }
132
133
0
  if (maximumCacheValidity > 0)
134
0
  {
135
0
    df = std::make_shared<CachedDateFormat>(df, maximumCacheValidity);
136
0
  }
137
138
0
  return df;
139
0
}
140
141
PatternConverterPtr DatePatternConverter::newInstance(
142
  const std::vector<LogString>& options)
143
0
{
144
0
  return std::make_shared<DatePatternConverter>(options);
145
0
}
146
147
void DatePatternConverter::format(
148
  const LoggingEventPtr& event,
149
  LogString& toAppendTo,
150
  Pool& p) const
151
0
{
152
0
  priv->df->format(toAppendTo, event->getTimeStamp(), p);
153
0
}
154
155
/**
156
 * {@inheritDoc}
157
 */
158
void DatePatternConverter::format(
159
  const ObjectPtr& obj,
160
  LogString& toAppendTo,
161
  Pool& p) const
162
0
{
163
0
  DatePtr date = LOG4CXX_NS::cast<Date>(obj);
164
165
0
  if (date != NULL)
166
0
  {
167
0
    format(date, toAppendTo, p);
168
0
  }
169
0
  else
170
0
  {
171
0
    LoggingEventPtr event = LOG4CXX_NS::cast<LoggingEvent>(obj);
172
173
0
    if (event != NULL)
174
0
    {
175
0
      format(event, toAppendTo, p);
176
0
    }
177
0
  }
178
0
}
179
180
/**
181
 * Append formatted date to string buffer.
182
 * @param date date
183
 * @param toAppendTo buffer to which formatted date is appended.
184
 */
185
void DatePatternConverter::format(
186
  const DatePtr& date,
187
  LogString& toAppendTo,
188
  Pool& p) const
189
0
{
190
0
  priv->df->format(toAppendTo, date->getTime(), p);
191
0
}