Coverage Report

Created: 2026-01-09 06:23

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
84.0k
    PatternConverterPrivate(name, style),
41
84.0k
    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
84.0k
  LoggingEventPatternConverter (std::make_unique<DatePatternConverterPrivate>(LOG4CXX_STR("Class Name"),
55
84.0k
      LOG4CXX_STR("class name"), getDateFormat(options)))
56
84.0k
{
57
84.0k
}
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
84.0k
  LoggingEventPatternConverter (std::make_unique<DatePatternConverterPrivate>(LOG4CXX_STR("Class Name"),
55
84.0k
      LOG4CXX_STR("class name"), getDateFormat(options)))
56
84.0k
{
57
84.0k
}
58
59
84.0k
DatePatternConverter::~DatePatternConverter() {}
60
61
DateFormatPtr DatePatternConverter::getDateFormat(const OptionsList& options)
62
84.0k
{
63
84.0k
  DateFormatPtr df;
64
84.0k
  int maximumCacheValidity = 1000000;
65
66
84.0k
  if (options.size() == 0)
67
62.9k
  {
68
62.9k
    df = std::make_shared<ISO8601DateFormat>();
69
62.9k
  }
70
21.1k
  else
71
21.1k
  {
72
21.1k
    LogString dateFormatStr(options[0]);
73
74
21.1k
    if (dateFormatStr.empty() ||
75
8.18k
      StringHelper::equalsIgnoreCase(dateFormatStr,
76
8.18k
        LOG4CXX_STR("ISO8601"), LOG4CXX_STR("iso8601")))
77
12.9k
    {
78
12.9k
      df = std::make_shared<ISO8601DateFormat>();
79
12.9k
    }
80
8.18k
    else if (StringHelper::equalsIgnoreCase(dateFormatStr,
81
8.18k
        LOG4CXX_STR("ABSOLUTE"), LOG4CXX_STR("absolute")))
82
0
    {
83
0
      df = std::make_shared<AbsoluteTimeDateFormat>();
84
0
    }
85
8.18k
    else if (StringHelper::equalsIgnoreCase(dateFormatStr,
86
8.18k
        LOG4CXX_STR("DATE"), LOG4CXX_STR("date")))
87
952
    {
88
952
      df = std::make_shared<DateTimeDateFormat>();
89
952
    }
90
7.23k
    else
91
7.23k
    {
92
7.23k
      if (dateFormatStr.find(0x25 /*'%'*/) == std::string::npos)
93
4.38k
      {
94
4.38k
        try
95
4.38k
        {
96
4.38k
          df = std::make_shared<SimpleDateFormat>(dateFormatStr);
97
4.38k
          maximumCacheValidity =
98
4.38k
            CachedDateFormat::getMaximumCacheValidity(dateFormatStr);
99
4.38k
        }
100
4.38k
        catch (std::exception& e)
101
4.38k
        {
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.38k
      }
108
2.85k
      else
109
2.85k
      {
110
2.85k
        df = std::make_shared<StrftimeDateFormat>(dateFormatStr);
111
2.85k
      }
112
7.23k
    }
113
114
21.1k
    if (options.size() >= 2)
115
13.8k
    {
116
13.8k
      TimeZonePtr tz;
117
13.8k
      try
118
13.8k
      {
119
13.8k
        tz = TimeZone::getTimeZone(options[1]);
120
13.8k
      }
121
13.8k
      catch (std::exception& e)
122
13.8k
      {
123
6.87k
        LogLog::warn(LOG4CXX_STR("Invalid time zone: ") + options[1], e);
124
6.87k
      }
125
126
13.8k
      if (tz)
127
6.96k
      {
128
6.96k
        df->setTimeZone(tz);
129
6.96k
      }
130
13.8k
    }
131
21.1k
  }
132
133
84.0k
  if (maximumCacheValidity > 0)
134
84.0k
  {
135
84.0k
    df = std::make_shared<CachedDateFormat>(df, maximumCacheValidity);
136
84.0k
  }
137
138
84.0k
  return df;
139
84.0k
}
140
141
PatternConverterPtr DatePatternConverter::newInstance(
142
  const std::vector<LogString>& options)
143
84.0k
{
144
84.0k
  return std::make_shared<DatePatternConverter>(options);
145
84.0k
}
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
}