/// @file /// @author rfree (current maintainer in monero.cc project) /// @brief various general utils taken from (and relate to) otshell project, including loggiang/debug /* See other files here for the LICENCE that applies here. */ #include "ccolor.hpp" #ifndef INCLUDE_OT_NEWCLI_UTILS #define INCLUDE_OT_NEWCLI_UTILS #include "lib_common1.hpp" #ifdef __unix #include #endif #if defined(_WIN32) #include"windows_stream.h" #endif #ifndef CFG_WITH_TERMCOLORS //#error "You requested to turn off terminal colors (CFG_WITH_TERMCOLORS), however currently they are hardcoded (this option to turn them off is not yet implemented)." #endif ///Macros related to automatic deduction of class name etc; #define MAKE_CLASS_NAME(NAME) private: static std::string GetObjectName() { return #NAME; } #define MAKE_STRUCT_NAME(NAME) private: static std::string GetObjectName() { return #NAME; } public: // define this to debug the debug system itself: // #define opt_debug_debug #ifdef opt_debug_debug #define _dbg_dbg(X) do { std::cerr<<"_dbg_dbg: " << OT_CODE_STAMP << " {thread=" << std::this_thread::get_id()<<"} " \ << " {pid="< std::string ToStr(const T & obj) { std::ostringstream oss; oss << obj; return oss.str(); } struct cNullstream : std::ostream { cNullstream() : std::ios(0), std::ostream(0) {} }; extern cNullstream g_nullstream; // a stream that does nothing (eats/discards data) // ========== debug ========== // _dbg_ignore is moved to global namespace (on purpose) // TODO make _dbg_ignore thread-safe everywhere extern std::recursive_mutex gLoggerGuard; // the mutex guarding logging/debugging code e.g. protecting streams, files, etc std::atomic & gLoggerGuardDepth_Get(); // getter for the global singleton of counter (it guarantees initializing it to 0). This counter shows the current recursion (re-entrant) level of debug macros. // TODO more debug of the debug system: // detect lock() error e.g. recursive limit // detect stream e.g. operator<< error #define _debug_level(LEVEL,VAR) do { if (_dbg_ignore< LEVEL) { \ _dbg_dbg("WRITE DEBUG: LEVEL="< mutex_guard( nOT::nUtils::gLoggerGuard ); \ part=1; \ try { \ ++nOT::nUtils::gLoggerGuardDepth_Get(); \ /* int counter = nOT::nUtils::gLoggerGuardDepth_Get(); if (counter!=1) gCurrentLogger.write_stream(100,"")<<"DEBUG-ERROR: recursion, counter="< mutex_guard( nOT::nUtils::gLoggerGuard ); \ part=1; \ try { \ ++nOT::nUtils::gLoggerGuardDepth_Get(); \ std::ostringstream oss; \ oss << nOT::nUtils::get_current_time() << ' ' << OT_CODE_STAMP << ' ' << VAR << gCurrentLogger.endline() << std::flush; \ std::string as_string = oss.str(); \ _dbg_dbg("START will write to log LEVEL="< mOutfile; std::ostream * mStream; ///< pointing only! can point to our own mOutfile, or maye to global null stream std::ostream * mStreamBrokenDebug; ///< pointing only! this is a pointer to some stream that should be used when normal debugging is broken eg std::cerr bool mIsBroken; ///< is the debugging system broken (this should be set when internal problems occur and should cause fallback to std::cerr) std::map< std::string , std::ofstream * > mChannels; // the ofstream objects are owned by this class int mLevel; ///< current debug level std::ostream & SelectOutput(int level, const std::string & channel) noexcept; ///< returns a proper stream for this level and channel (always usable string) void OpenNewChannel(const std::string & channel) noexcept; ///< tries to prepare this channel. does NOT guarantee to created mChannels[] entry! void OpenNewChannel_(const std::string & channel); ///< internal function, will throw in case of problems std::string GetLogBaseDir() const; std::map< std::thread::id , int > mThread2Number; ///< change long thread IDs into a short nice number to show int mThread2Number_Biggest; ///< current biggest value held there (biggest key) - works as growing-only counter basically int Thread2Number(const std::thread::id id); ///< convert the system's thread id into a nice short our id; make one if new thread std::map< t_anypid , int > mPid2Number; ///< change long proces PID into a short nice number to show int mPid2Number_Biggest; ///< current biggest value held there (biggest key) - works as growing-only counter basically int Pid2Number(const t_anypid id); ///< convert the system's PID id into a nice short our id; make one if new thread }; // ==================================================================== // vector debug template std::string vectorToStr(const T & v) { std::ostringstream oss; for(auto rec: v) { oss << rec <<","; } return oss.str(); } template void DisplayVector(std::ostream & out, const std::vector &v, const std::string &delim=" ") { std::copy( v.begin(), v.end(), std::ostream_iterator(out, delim.c_str()) ); } template void EndlDisplayVector(std::ostream & out, const std::vector &v, const std::string &delim=" ") { out << std::endl; DisplayVector(out,v,delim); } template void DisplayVectorEndl(std::ostream & out, const std::vector &v, const std::string &delim=" ") { DisplayVector(out,v,delim); out << std::endl; } template void DbgDisplayVector(const std::vector &v, const std::string &delim=" ") { std::cerr << "["; std::copy( v.begin(), v.end(), std::ostream_iterator(std::cerr, delim.c_str()) ); std::cerr << "]"; } string stringToColor(const string &hash); template void DisplayMap(std::ostream & out, const std::map &m, const std::string &delim=" ") { auto *no_color = zkr::cc::fore::console; for(auto var : m) { out << stringToColor(var.first) << var.first << delim << var.second << no_color << endl; } } template void EndlDisplayMap(std::ostream & out, const std::map &m, const std::string &delim=" ") { out << endl; for(auto var : m) { out << var.first << delim << var.second << endl; } } template void DbgDisplayMap(const std::map &m, const std::string &delim=" ") { for(auto var : m) { std::cerr << var.first << delim << var.second << endl; } } template void DbgDisplayVectorEndl(const std::vector &v, const std::string &delim=" ") { DbgDisplayVector(v,delim); std::cerr << std::endl; } void DisplayStringEndl(std::ostream & out, const std::string text); bool CheckIfBegins(const std::string & beggining, const std::string & all); bool CheckIfEnds (std::string const & ending, std::string const & all); std::string SpaceFromEscape(const std::string &s); std::string EscapeFromSpace(const std::string &s); vector WordsThatMatch(const std::string & sofar, const vector & possib); char GetLastChar(const std::string & str); std::string GetLastCharIf(const std::string & str); // TODO unicode? std::string EscapeString(const std::string &s); template std::string DbgVector(const std::vector &v, const std::string &delim="|") { std::ostringstream oss; oss << "["; bool first=true; for(auto vElement : v) { if (!first) oss<(oss, delim.c_str()) ); return oss.str(); } template std::ostream & operator<<(std::ostream & os, const map< T, vector > & obj){ os << "["; for(auto const & elem : obj) { os << " [" << elem.first << "=" << DbgVector(elem.second) << "] "; } os << "]"; return os; } template std::string DbgMap(const map & map) { std::ostringstream oss; oss << map; return oss.str(); } // ==================================================================== // assert // ASRT - assert. Name like ASSERT() was too long, and ASS() was just... no. // Use it like this: ASRT( x>y ); with the semicolon at end, a clever trick forces this syntax :) #define ASRT(x) do { if (!(x)) nOT::nUtils::Assert(false, OT_CODE_STAMP, #x); } while(0) void Assert(bool result, const std::string &stamp, const std::string &condition); // ==================================================================== // advanced string const std::string GetMultiline(string endLine = "~"); vector SplitString(const string & str); bool checkPrefix(const string & str, char prefix = '^'); // ==================================================================== // nUse utils enum class eSubjectType {Account, Asset, User, Server, Unknown}; string SubjectType2String(const eSubjectType & type); eSubjectType String2SubjectType(const string & type); // ==================================================================== // operation on files /// @brief tools related to filesystem /// @author rfree (maintainer) class cFilesystemUtils { // if we do not want to use boost in given project (or we could optionally write boost here later) public: static bool CreateDirTree(const std::string & dir, bool only_below=false); static char GetDirSeparatorSys(); /// < eg '/' or '\' static char GetDirSeparatorInter(); /// < internal is '/' static string FileInternalToSystem(const std::string &name); ///< converts from internal file name string to system file name string static string FileSystemToInternal(const std::string &name); ///< converts from system file name string to internal file name string }; /// @brief utils to e.g. edit a file from console /// @author rfree (maintainer) class cEnvUtils { int fd; string mFilename; void GetTmpTextFile(); void CloseFile(); void OpenEditor(); const string ReadFromTmpFile(); public: const string Compose(); const string ReadFromFile(const string path); }; void hintingToTxt(std::fstream & file, string command, vector &commands); void generateQuestions (std::fstream & file, string command); void generateAnswers (std::fstream & file, string command, vector &completions); // ==================================================================== namespace nOper { // nOT::nUtils::nOper // cool shortcut operators, like vector + vecotr operator working same as string (appending) // isolated to namespace because it's unorthodox ide to implement this using namespace std; // TODO use && and move? template vector operator+(const vector &a, const vector &b) { vector ret = a; ret.insert( ret.end() , b.begin(), b.end() ); return ret; } template vector operator+(const T &a, const vector &b) { vector ret(1,a); ret.insert( ret.end() , b.begin(), b.end() ); return ret; } template vector operator+(const vector &a, const T &b) { vector b_vector(1,a); return a + b_vector; } template vector& operator+=(vector &a, const vector &b) { a.insert( a.end() , b.begin(), b.end() ); return a; } // map template map operator+(const map &a, const map &b) { map ret = a; for (const auto & elem : b) { ret.insert(elem); } return ret; } } // nOT::nUtils::nOper // ==================================================================== // ==================================================================== // Algorithms // ==================================================================== // ==================================================================== /** @brief Special type that on creation will be initialized to have value INIT given as template argument. Might be usefull e.g. to express in the declaration of class what will be the default value of member variable See also http://www.boost.org/doc/libs/1_56_0/libs/utility/value_init.htm Probably not needed when using boost in your project. */ template class value_init { private: T data; public: value_init(); T& operator=(const T& v) { data=v; return *this; } operator T const &() const { return data; } operator T&() { return data; } }; template value_init::value_init() : data(INIT) { } } // namespace nUtils } // namespace nOT // global namespace extern nOT::nUtils::cLogger gCurrentLogger; ///< The current main logger. Usually do not use it directly, instead use macros like _dbg1 etc std::string GetObjectName(); ///< Method to return name of current object; To use in debug; Can be shadowed in your classes. (Might be not used currently) const extern int _dbg_ignore; ///< the global _dbg_ignore, but local code (blocks, classes etc) you could shadow it in your code blocks, // to override debug compile-time setting for given block/class, e.g. to disable debug in one of your methods or increase it there. // Or to make it runtime by providing a class normal member and editing it in runtime #define OT_CODE_STAMP ( nOT::nUtils::ToStr("[") + nOT::nUtils::nDetail::DbgShortenCodeFileName(__FILE__) + nOT::nUtils::ToStr("+") + nOT::nUtils::ToStr(__LINE__) + nOT::nUtils::ToStr(" ") + (GetObjectName()) + nOT::nUtils::ToStr("::") + nOT::nUtils::ToStr(__FUNCTION__) + nOT::nUtils::ToStr("]")) #endif