<commit_before>/*
 *  Author(s):
 *  - <NAME> <<EMAIL>>
 *  - <NAME> <<EMAIL>>
 *
 *  Copyright (C) 2010, 2011 Aldebaran Robotics
 */

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <qi/os.hpp>
#include <qi/log.hpp>
#include <qi/log/consoleloghandler.hpp>

#ifdef _WIN32
# include <windows.h>
# include <io.h>
# define isatty _isatty
#else
# include <unistd.h>
#endif

#define CATSIZEMAX 16

namespace qi {
  namespace log {
    ConsoleLogHandler::ConsoleLogHandler()
      : _color(1)
    {
#ifdef WIN32
      _winScreenHandle = GetStdHandle(STD_OUTPUT_HANDLE);
#endif
      const char *verbose = std::getenv("VERBOSE");
      const char *context = std::getenv("CONTEXT");
      const char *color   = std::getenv("CLICOLOR");


      if (verbose)
        qi::log::setVerbosity((LogLevel)atoi(verbose));
      if (context)
        qi::log::setContext(atoi(context) > 0 ? true: false);
      if (color)
        _color = atoi(color) > 0 ? true: false;
      if (!isatty(1))
        _color = 0;
    }

    void cutCat(const char* category, char* res)
    {
	  int categorySize = strlen(category); 
	  if (categorySize < CATSIZEMAX)
      {
        memset(res, ' ', CATSIZEMAX);
        memcpy(res, category, strlen(category));
      }
      else
      {
        memset(res, '.', CATSIZEMAX);
        memcpy(res + 3, category + categorySize - CATSIZEMAX + 3, CATSIZEMAX - 3);
      }
      res[CATSIZEMAX] = '\0';
    }

    void ConsoleLogHandler::log(const LogLevel    verb,
                                const char       *file,
                                const char       *fct,
                                const char       *category,
                                const int         line,
                                const char       *msg)
    {
      if (verb > qi::log::getVerbosity())
      {
        return;
      }
      else
      {
        header(verb);
        char fixedCategory[CATSIZEMAX + 1];
		fixedCategory[CATSIZEMAX] = '\0';
        cutCat(category, fixedCategory);
        if (qi::log::getContext() != 0)
        {
          printf("%s: %s(%d) %s %s", fixedCategory, file, line, fct, msg);
          fflush (stdout);
        }
        else
        {
          printf("%s: ", fixedCategory);
          if (qi::log::getContext())
          {
            printf("%s(%d) %s ", file, line, fct);
          }
          printf("%s", msg);
          fflush (stdout);
        }
      }
      fflush (stdout);
    }

    void ConsoleLogHandler::textColor(char fg, char bg, char attr) const
    {
      if (!_color)
        return;
#ifdef _WIN32
      if (fg == reset) 
      {
        SetConsoleTextAttribute(_winScreenHandle, whitegray);
      }
      else
      {
        SetConsoleTextAttribute(_winScreenHandle, fg);
      }
      return;
#endif
      if (attr != -1 && bg != -1)
        printf("%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
      else if (bg != -1)
        printf("%c[%d;%dm", 0x1B, fg + 30, bg + 40);
      else
        printf("%c[%dm", 0x1B, fg + 30);
    }

    void ConsoleLogHandler::textColorBG(char bg) const
    {
      if (!_color)
        return;
#ifdef _WIN32
      return;
#endif
      printf("%c[%dm", 0x1B, bg + 40);
    }

    void ConsoleLogHandler::textColorAttr(char attr) const
    {
      if (!_color)
        return;

#ifdef _WIN32
      if (attr == reset) 
      {
        SetConsoleTextAttribute(_winScreenHandle, whitegray);
      }
      else
      {
        SetConsoleTextAttribute(_winScreenHandle, attr);
      }
      return;
#endif
      printf("%c[%dm", 0x1B, attr);
    }

    void ConsoleLogHandler::header(const LogLevel verb) const
    {
      //display log level
      textColorAttr(bright);
      if (verb == fatal)
        textColor(magenta);
      if (verb == error)
        textColor(red);
      if (verb == warning)
        textColor(yellow);
      if (verb == info)
        textColor(white);
      if (verb == verbose)<commit_msg>libqi: Same color on windows and linux (console log handler)<commit_after>/*
 *  Author(s):
 *  - <NAME> <<EMAIL>>
 *  - <NAME> <<EMAIL>>
 *
 *  Copyright (C) 2010, 2011 Aldebaran Robotics
 */

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <qi/os.hpp>
#include <qi/log.hpp>
#include <qi/log/consoleloghandler.hpp>

#ifdef _WIN32
# include <windows.h>
# include <io.h>
# define isatty _isatty
#else
# include <unistd.h>
#endif

#define CATSIZEMAX 16

namespace qi {
  namespace log {
    ConsoleLogHandler::ConsoleLogHandler()
      : _color(1)
    {
#ifdef _WIN32
      _winScreenHandle = GetStdHandle(STD_OUTPUT_HANDLE);
#endif
      const char *verbose = std::getenv("VERBOSE");
      const char *context = std::getenv("CONTEXT");
      const char *color   = std::getenv("CLICOLOR");


      if (verbose)
        qi::log::setVerbosity((LogLevel)atoi(verbose));
      if (context)
        qi::log::setContext(atoi(context) > 0 ? true: false);
      if (color)
        _color = atoi(color) > 0 ? true: false;
      if (!isatty(1))
        _color = 0;
    }

    void cutCat(const char* category, char* res)
    {
      int categorySize = strlen(category);
      if (categorySize < CATSIZEMAX)
      {
        memset(res, ' ', CATSIZEMAX);
        memcpy(res, category, strlen(category));
      }
      else
      {
        memset(res, '.', CATSIZEMAX);
        memcpy(res + 3, category + categorySize - CATSIZEMAX + 3, CATSIZEMAX - 3);
      }
      res[CATSIZEMAX] = '\0';
    }

    void ConsoleLogHandler::log(const LogLevel    verb,
                                const char       *file,
                                const char       *fct,
                                const char       *category,
                                const int         line,
                                const char       *msg)
    {
      if (verb > qi::log::getVerbosity())
      {
        return;
      }
      else
      {
        header(verb);
        char fixedCategory[CATSIZEMAX + 1];
        fixedCategory[CATSIZEMAX] = '\0';
        cutCat(category, fixedCategory);
        if (qi::log::getContext() != 0)
        {
          printf("%s: %s(%d) %s %s", fixedCategory, file, line, fct, msg);
          fflush (stdout);
        }
        else
        {

#ifndef WIN32
          textColorAttr(reset);
          textColor(gray);
#endif
          printf("%s: ", fixedCategory);
          if (qi::log::getContext())
          {
            printf("%s(%d) %s ", file, line, fct);
          }
          printf("%s", msg);
          fflush (stdout);
        }
      }
      fflush (stdout);
    }

    void ConsoleLogHandler::textColor(char fg, char bg, char attr) const
    {
      if (!_color)
        return;
#ifdef _WIN32
      if (fg == reset)
      {
        SetConsoleTextAttribute(_winScreenHandle, whitegray);
      }
      else
      {
        SetConsoleTextAttribute(_winScreenHandle, fg);
      }
      return;
#endif
      if (attr != -1 && bg != -1)
        printf("%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
      else if (bg != -1)
        printf("%c[%d;%dm", 0x1B, fg + 30, bg + 40);
      else
        printf("%c[%dm", 0x1B, fg + 30);
    }

    void ConsoleLogHandler::textColorBG(char bg) const
    {
      if (!_color)
        return;
#ifdef _WIN32
      return;
#endif
      printf("%c[%dm", 0x1B, bg + 40);
    }

    void ConsoleLogHandler::textColorAttr(char attr) const
    {
      if (!_color)
        return;

#ifdef _WIN32
      if (attr == reset)
      {
        SetConsoleTextAttribute(_winScreenHandle, whitegray);
      }
      else
      {
        SetConsoleTextAttribute(_winScreenHandle, attr);
      }
      return;
#endif
      printf("%c[%dm", 0x1B, attr);
    }

    void ConsoleLogHandler::header(const LogLevel verb) const
    {
      //display log level
      textColorAttr(bright);
      if (verb == fatal)
        textColor(magenta);
      if (verb == error)
        textColor(red);
      if (verb == warning)
        textColor(yellow);
      if (verb == info)
        textColor(white);
      if (verb == verbose)
{'fraction_non_alphanumeric': 0.1360229827629278, 'fraction_numerical': 0.014489133150137397, 'mean_word_length': 1.8545454545454545, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2941811', 'n_tokens_mistral': 2708, 'n_tokens_neox': 2594, 'n_words': 593}