Skip to content

Commit d385fc6

Browse files
committed
Move header formatting from stream to logger
1 parent 26a6716 commit d385fc6

File tree

5 files changed

+105
-16
lines changed

5 files changed

+105
-16
lines changed

src/app/BlockDecompressor.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ int BlockDecompressor::decompress(uint64& inputSize)
8989

9090
if (isStdIn == false) {
9191
vector<string> errors;
92-
bool isRecursive = (_inputName.length() < 2) || (_inputName[_inputName.length() - 2] != PATH_SEPARATOR) ||
93-
(_inputName[_inputName.length() - 1] != '.');
92+
bool isRecursive = (_inputName.length() < 2) ||
93+
(_inputName[_inputName.length() - 2] != PATH_SEPARATOR) ||
94+
(_inputName[_inputName.length() - 1] != '.');
9495
FileListConfig cfg = { isRecursive, _noLinks, false, _noDotFiles };
9596
createFileList(_inputName, files, cfg, errors);
9697

src/app/InfoPrinter.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
#include <ios>
1818
#include <sstream>
1919
#include "InfoPrinter.hpp"
20+
#include "../util.hpp"
2021

2122
using namespace kanzi;
2223
using namespace std;
@@ -135,8 +136,34 @@ void InfoPrinter::processEvent(const Event& evt)
135136
delete bi;
136137
_map[hash(currentBlockId)] = nullptr;
137138
}
138-
else if ((evt.getType() == Event::AFTER_HEADER_DECODING) && (_level >= 3)) {
139-
_os << evt.toString() << endl;
139+
else if (evt.getType() == Event::AFTER_HEADER_DECODING) {
140+
if (_level >= 3) {
141+
stringstream ss(evt.toString());
142+
string s = ss.str();
143+
vector<string> tokens;
144+
const int nbTokens = tokenizeCSV(s, tokens);
145+
ss.str(string());
146+
147+
if (nbTokens > 1)
148+
ss << "Bitstream version: " << tokens[1] << endl;
149+
150+
if (nbTokens > 2)
151+
ss << "Block checksum: " << tokens[2] << (tokens[2] == "NONE" ? "" : " bits") << endl;
152+
153+
if (nbTokens > 3)
154+
ss << "Block size: " << tokens[3] << " bytes" << endl;
155+
156+
if (nbTokens > 4)
157+
ss << "Using " << (tokens[4] == "" ? "no" : tokens[4]) << " entropy codec (stage 1)" << endl;
158+
159+
if (nbTokens > 5)
160+
ss << "Using " << (tokens[5] == "" ? "no" : tokens[5]) << " transform (stage 2)" << endl;
161+
162+
if (nbTokens > 7)
163+
ss << "Original size: " << tokens[7] << " byte(s)" << endl;;
164+
165+
_os << ss.str() << endl;
166+
}
140167
}
141168
else if (_level >= 5) {
142169
_os << evt.toString() << endl;

src/app/InfoPrinter.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ namespace kanzi
3838
public:
3939
enum Type {
4040
ENCODING,
41-
DECODING
41+
DECODING,
42+
INFO
4243
};
4344

4445
InfoPrinter(int infoLevel, InfoPrinter::Type type, OutputStream& os);
@@ -61,7 +62,7 @@ namespace kanzi
6162
Clock _clock12;
6263
Clock _clock23;
6364
Clock _clock34;
64-
65+
6566
static uint hash(uint id) { return (id * 0x1E35A7BD) & 0x03FF; }
6667
};
6768
}

src/io/CompressedInputStream.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -376,24 +376,27 @@ void CompressedInputStream::readHeader()
376376

377377
if (_listeners.size() > 0) {
378378
stringstream ss;
379-
ss << "Bitstream version: " << bsVersion << endl;
379+
string inputName = _ctx.getString("inputName", "");
380+
ss << inputName << ",";
381+
ss << bsVersion << ",";
380382
string ckSize = "NONE";
381383

382384
if (_hasher32 != nullptr)
383-
ckSize = "32 bits";
385+
ckSize = "32";
384386
else if (_hasher64 != nullptr)
385-
ckSize = "64 bits";
387+
ckSize = "64";
386388

387-
ss << "Block checksum: " << ckSize<< endl;
388-
ss << "Block size: " << _blockSize << " bytes" << endl;
389+
ss << ckSize << ",";
390+
ss << _blockSize << ",";
389391
string w1 = EntropyDecoderFactory::getName(_entropyType);
390-
ss << "Using " << ((w1 == "NONE") ? "no" : w1) << " entropy codec (stage 1)" << endl;
392+
ss << ((w1 == "NONE") ? "" : w1) << ",";
391393
string w2 = TransformFactory<byte>::getName(_transformType);
392-
ss << "Using " << ((w2 == "NONE") ? "no" : w2) << " transform (stage 2)" << endl;
394+
ss << ((w2 == "NONE") ? "" : w2) << ",";
395+
long fileSize = _ctx.getLong("fileSize", 0);
396+
ss << fileSize << ",";
393397

394398
if (szMask != 0) {
395-
ss << "Original size: " << _outputSize;
396-
ss << (_outputSize < 2 ? " byte" : " bytes") << endl;
399+
ss << _outputSize << ",";
397400
}
398401

399402
// Protect against future concurrent modification of the list of block listeners

src/util.hpp

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ limitations under the License.
1818
#define _util_
1919

2020

21+
#include <cstdlib>
22+
#include <iomanip>
2123
#include <iostream>
24+
#include <sstream>
25+
#include <string>
26+
#include <vector>
2227
#include "types.hpp"
2328

2429

@@ -42,5 +47,57 @@ struct istreambuf : public std::basic_streambuf<T, std::char_traits<T> >
4247
}
4348
};
4449

45-
#endif
50+
inline int tokenizeCSV(std::string& s, std::vector<std::string>& tokens, char delim = ',')
51+
{
52+
std::stringstream ss;
53+
char prv = 0;
54+
55+
for (auto& c : s) {
56+
if (c == delim) {
57+
if (prv != '\\') {
58+
std::string tk = ss.str();
59+
tokens.push_back(tk);
60+
ss.str("");
61+
prv = 0;
62+
continue;
63+
}
64+
}
65+
66+
ss << c;
67+
prv = c;
68+
}
69+
70+
std::string tk = ss.str();
4671

72+
if (tk.size() > 0)
73+
tokens.push_back(tk);
74+
75+
return tokens.size();
76+
}
77+
78+
inline std::string formatSize(const std::string& input)
79+
{
80+
double size = atof(input.c_str());;
81+
std::stringstream ss;
82+
std::string s = input;
83+
84+
if (size >= double(1 << 30)) {
85+
size /= double(1024 * 1024 * 1024);
86+
ss << std::fixed << std::setprecision(2) << size << " GiB";
87+
s = ss.str();
88+
}
89+
else if (size >= double(1 << 20)) {
90+
size /= double(1024 * 1024);
91+
ss << std::fixed << std::setprecision(2) << size << " MiB";
92+
s = ss.str();
93+
}
94+
else if (size >= double(1 << 10)) {
95+
size /= double(1024);
96+
ss << std::fixed << std::setprecision(2) << size << " KiB";
97+
s = ss.str();
98+
}
99+
100+
return s;
101+
}
102+
103+
#endif

0 commit comments

Comments
 (0)