Line data Source code
1 : #include "Log/ProgressLoggerJsonOStream.hpp" 2 : 3 : #include <httplib.h> 4 : 5 : #include <ostream> 6 : #include <stdexcept> 7 : 8 : using namespace std; 9 : using namespace Log; 10 : 11 0 : ProgressLoggerJsonOStream::ProgressLoggerJsonOStream(ostream &os_): 12 0 : os(os_) {} 13 : 14 0 : ProgressLoggerJsonOStream &ProgressLoggerJsonOStream::operator<<(const StartMessage &) { 15 0 : if(s != NO_MESSAGE) 16 0 : throw logic_error("Cannot start message if state is not NO_MESSAGE"); 17 : 18 0 : s = MESSAGE; 19 0 : return *this; 20 : } 21 : 22 0 : ProgressLoggerJsonOStream &ProgressLoggerJsonOStream::operator<<(const EndMessage &) { 23 0 : if(s == TEXT) 24 0 : *this << EndText(); 25 : 26 0 : if(s != MESSAGE) 27 0 : throw logic_error("Cannot end message if state is not MESSAGE"); 28 : 29 0 : os << jsonMessage.dump() << endl; 30 0 : jsonMessage.clear(); 31 : 32 0 : s = NO_MESSAGE; 33 0 : firstField = true; 34 : 35 0 : return *this; 36 : } 37 : 38 0 : ProgressLoggerJsonOStream &ProgressLoggerJsonOStream::operator<<(const Progress &progress) { 39 0 : if(s == NO_MESSAGE) 40 0 : *this << StartMessage(); 41 : 42 0 : if(s != MESSAGE) 43 0 : throw logic_error("Cannot send Progress if state is not MESSAGE"); 44 : 45 0 : jsonMessage["progress"] = progress.p; 46 : 47 0 : return *this; 48 : } 49 : 50 0 : ProgressLoggerJsonOStream &ProgressLoggerJsonOStream::operator<<(const Elapsed &elapsed) { 51 0 : if(s == NO_MESSAGE) 52 0 : *this << StartMessage(); 53 : 54 0 : if(s != MESSAGE) 55 0 : throw logic_error("Cannot send Progress if state is not MESSAGE"); 56 : 57 0 : jsonMessage["elapsed"] = elapsed.t; 58 : 59 0 : return *this; 60 : } 61 : 62 0 : ProgressLoggerJsonOStream &ProgressLoggerJsonOStream::operator<<(const ETA &eta) { 63 0 : if(s == NO_MESSAGE) 64 0 : *this << StartMessage(); 65 : 66 0 : if(s != MESSAGE) 67 0 : throw logic_error("Cannot send ETA if state is not MESSAGE"); 68 : 69 0 : jsonMessage["eta"] = eta.t; 70 : 71 0 : return *this; 72 : } 73 : 74 0 : ProgressLoggerJsonOStream &ProgressLoggerJsonOStream::operator<<(const StartText &) { 75 0 : if(s == NO_MESSAGE) 76 0 : *this << StartMessage(); 77 0 : if(s != MESSAGE) 78 0 : throw logic_error("Cannot start text if state is not MESSAGE"); 79 : 80 0 : s = TEXT; 81 : 82 0 : textStream.str(""); 83 0 : textStream.clear(); 84 : 85 0 : return *this; 86 : } 87 : 88 0 : ProgressLoggerJsonOStream &ProgressLoggerJsonOStream::operator<<(const EndText &) { 89 0 : if(s != TEXT) 90 0 : throw logic_error("Cannot end text if state is not TEXT"); 91 : 92 0 : string text = textStream.str(); 93 : 94 0 : jsonMessage["message"] = text; 95 : 96 0 : s = MESSAGE; 97 : 98 0 : return *this; 99 : } 100 : 101 : // clang-format off 102 0 : ProgressLoggerJsonOStream &ProgressLoggerJsonOStream::operator<<(const int &t) { send(t); return *this; } 103 0 : ProgressLoggerJsonOStream &ProgressLoggerJsonOStream::operator<<(const unsigned long &t) { send(t); return *this; } 104 0 : ProgressLoggerJsonOStream &ProgressLoggerJsonOStream::operator<<(const double &t) { send(t); return *this; } 105 0 : ProgressLoggerJsonOStream &ProgressLoggerJsonOStream::operator<<(const char *t) { send(t); return *this; } 106 : 107 0 : ProgressLoggerJsonOStream &ProgressLoggerJsonOStream::operator<<(std::_Setprecision t) { os << t; textStream << t; return *this; } 108 : 109 0 : ProgressLoggerJsonOStream &ProgressLoggerJsonOStream::fixed() { os << std::fixed; textStream << std::fixed; return *this; } 110 : // clang-format on