No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

logger.cpp 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* The MIT License
  2. *
  3. * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "logger.h"
  24. #include "util.h"
  25. #include <iostream>
  26. #include <sstream>
  27. #include <cstdarg>
  28. #include <cstdlib>
  29. #include <boost/algorithm/string.hpp>
  30. void _log(int level, const char *trace, const char *format, ...)
  31. {
  32. va_list args;
  33. std::stringstream out;
  34. std::string strace;
  35. va_start(args, format);
  36. std::string text = vformat(format, args);
  37. va_end(args);
  38. if(trace) {
  39. strace = trace;
  40. strace = strace.substr(0, strace.find_first_of('('));
  41. if(strace.find_last_of(' ') != std::string::npos)
  42. strace = strace.substr(strace.find_last_of(' ') + 1);
  43. }
  44. #ifdef linux
  45. static char const *colors[] = { "\033[01;31m ", "\033[01;31m", "\033[01;33m", "\033[0;32m", "\033[01;34m" };
  46. static bool colored = getenv("COLORED_OUTPUT");
  47. if(colored)
  48. out << colors[level];
  49. #endif
  50. if(!strace.empty())
  51. out << "[" << strace << "] ";
  52. static char const *prefixes[] = { "FATAL ERROR: ", "ERROR: ", "WARNING: ", "", "", "" };
  53. out << prefixes[level];
  54. out << text;
  55. #ifdef linux
  56. if(colored)
  57. out << "\033[0m";
  58. #endif
  59. if(level <= LWARNING)
  60. std::cerr << out.str() << std::endl;
  61. else
  62. std::cout << out.str() << std::endl;
  63. if(level == LFATAL)
  64. exit(-1);
  65. }