SSP21-CPP
HexLogging.h
1 /*
2  * Copyright (c) 2019, Automatak LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
6  * following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
9  * disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
15  * products derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #ifndef LOG4CPP_HEXLOGGING_H
26 #define LOG4CPP_HEXLOGGING_H
27 
28 #include "ser4cpp/container/SequenceTypes.h"
29 #include "ser4cpp/util/HexConversions.h"
30 #include "ser4cpp/util/Uncopyable.h"
31 
32 #include "log4cpp/Location.h"
33 #include "log4cpp/Logger.h"
34 
35 namespace log4cpp
36 {
37 
39 {
40  static const uint32_t max_hex_per_line = max_log_entry_size / 3;
41 
42 public:
43  static void log(
44  Logger& logger,
45  LogLevel level,
46  const ser4cpp::rseq_t& source,
47  char separator = ' ',
48  uint32_t first_row_size = max_hex_per_line,
49  uint32_t other_row_size = max_hex_per_line
50  )
51  {
52  ser4cpp::rseq_t copy(source);
53  uint32_t row = 0;
54 
55  const auto max_first_size = ser4cpp::bounded<uint32_t>(first_row_size, 1, max_hex_per_line);
56  const auto max_other_size = ser4cpp::bounded<uint32_t>(other_row_size, 1, max_hex_per_line);
57 
58  while (copy.is_not_empty())
59  {
60  const auto row_size = (row == 0) ? max_first_size : max_other_size;
61  copy = log_line(logger, level, copy, separator, row_size);
62  ++row;
63  }
64  }
65 
66 private:
67  static ser4cpp::rseq_t log_line(
68  Logger& logger,
69  LogLevel level,
70  const ser4cpp::rseq_t& data,
71  char separator,
72  uint32_t max_row_size
73  )
74  {
75  char buffer[max_log_entry_size];
76 
77  uint32_t count = 0;
78 
79  while ((count < max_row_size) && (count < data.length()))
80  {
81  auto pos = count * 3;
82  buffer[pos] = ser4cpp::HexConversions::to_hex_char((data[count] & 0xF0) >> 4);
83  buffer[pos + 1] = ser4cpp::HexConversions::to_hex_char(data[count] & 0x0F);
84  buffer[pos + 2] = separator;
85  ++count;
86  }
87 
88  buffer[(3 * count) - 1] = '\0';
89 
90  logger.log(level, LOCATION, buffer);
91 
92  return data.skip(count);
93  }
94 };
95 
96 } //namespace log4cpp
97 
98 #endif //LOG4CPP_HEXLOGGING_H
log4cpp header-only library namespace