Coverage Report

Created: 2026-03-12 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/cpp/datepatternconverter.cpp
Line
Count
Source
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
76.8k
    PatternConverterPrivate(name, style),
41
76.8k
    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
76.8k
  LoggingEventPatternConverter (std::make_unique<DatePatternConverterPrivate>(LOG4CXX_STR("Class Name"),
55
76.8k
      LOG4CXX_STR("class name"), getDateFormat(options)))
56
76.8k
{
57
76.8k
}
Unexecuted instantiation: log4cxx::pattern::DatePatternConverter::DatePatternConverter(std::__1::vector<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >, std::__1::allocator<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > > > const&)
log4cxx::pattern::DatePatternConverter::DatePatternConverter(std::__1::vector<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >, std::__1::allocator<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > > > const&)
Line
Count
Source
54
76.8k
  LoggingEventPatternConverter (std::make_unique<DatePatternConverterPrivate>(LOG4CXX_STR("Class Name"),
55
76.8k
      LOG4CXX_STR("class name"), getDateFormat(options)))
56
76.8k
{
57
76.8k
}
58
59
76.8k
DatePatternConverter::~DatePatternConverter() {}
60
61
DateFormatPtr DatePatternConverter::getDateFormat(const OptionsList& options)
62
76.8k
{
63
76.8k
  DateFormatPtr df;
64
76.8k
  int maximumCacheValidity = 1000000;
65
66
76.8k
  if (options.size() == 0)
67
57.2k
  {
68
57.2k
    df = std::make_shared<ISO8601DateFormat>();
69
57.2k
  }
70
19.5k
  else
71
19.5k
  {
72
19.5k
    LogString dateFormatStr(options[0]);
73
74
19.5k
    if (dateFormatStr.empty() ||
75
6.90k
      StringHelper::equalsIgnoreCase(dateFormatStr,
76
6.90k
        LOG4CXX_STR("ISO8601"), LOG4CXX_STR("iso8601")))
77
12.6k
    {
78
12.6k
      df = std::make_shared<ISO8601DateFormat>();
79
12.6k
    }
80
6.90k
    else if (StringHelper::equalsIgnoreCase(dateFormatStr,
81
6.90k
        LOG4CXX_STR("ABSOLUTE"), LOG4CXX_STR("absolute")))
82
0
    {
83
0
      df = std::make_shared<AbsoluteTimeDateFormat>();
84
0
    }
85
6.90k
    else if (StringHelper::equalsIgnoreCase(dateFormatStr,
86
6.90k
        LOG4CXX_STR("DATE"), LOG4CXX_STR("date")))
87
277
    {
88
277
      df = std::make_shared<DateTimeDateFormat>();
89
277
    }
90
6.62k
    else
91
6.62k
    {
92
6.62k
      if (dateFormatStr.find(0x25 /*'%'*/) == std::string::npos)
93
4.09k
      {
94
4.09k
        try
95
4.09k
        {
96
4.09k
          df = std::make_shared<SimpleDateFormat>(dateFormatStr);
97
4.09k
          maximumCacheValidity =
98
4.09k
            CachedDateFormat::getMaximumCacheValidity(dateFormatStr);
99
4.09k
        }
100
4.09k
        catch (std::exception& e)
101
4.09k
        {
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
4.09k
      }
108
2.52k
      else
109
2.52k
      {
110
2.52k
        df = std::make_shared<StrftimeDateFormat>(dateFormatStr);
111
2.52k
      }
112
6.62k
    }
113
114
19.5k
    if (options.size() >= 2)
115
13.7k
    {
116
13.7k
      TimeZonePtr tz;
117
13.7k
      try
118
13.7k
      {
119
13.7k
        tz = TimeZone::getTimeZone(options[1]);
120
13.7k
      }
121
13.7k
      catch (std::exception& e)
122
13.7k
      {
123
6.73k
        LogLog::warn(LOG4CXX_STR("Invalid time zone: ") + options[1], e);
124
6.73k
      }
125
126
13.7k
      if (tz)
127
6.98k
      {
128
6.98k
        df->setTimeZone(tz);
129
6.98k
      }
130
13.7k
    }
131
19.5k
  }
132
133
76.8k
  if (maximumCacheValidity > 0)
134
76.8k
  {
135
76.8k
    df = std::make_shared<CachedDateFormat>(df, maximumCacheValidity);
136
76.8k
  }
137
138
76.8k
  return df;
139
76.8k
}
140
141
PatternConverterPtr DatePatternConverter::newInstance(
142
  const std::vector<LogString>& options)
143
76.8k
{
144
76.8k
  return std::make_shared<DatePatternConverter>(options);
145
76.8k
}
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
}