<commit_before>/***************************************************************************
**                                                                        **
**   Copyright (C) 2009-2010 Nokia Corporation.                           **
**                                                                        **
**   Author: <NAME> <<EMAIL>>                    **
**   Author: <NAME> <<EMAIL>>                    **
**   Author: <NAME> <<EMAIL>>                **
**                                                                        **
**     This file is part of Timed                                         **
**                                                                        **
**     Timed is free software; you can redistribute it and/or modify      **
**     it under the terms of the GNU Lesser General Public License        **
**     version 2.1 as published by the Free Software Foundation.          **
**                                                                        **
**     Timed is distributed in the hope that it will be useful, but       **
**     WITHOUT ANY WARRANTY;  without even the implied warranty  of       **
**     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               **
**     See the GNU Lesser General Public License  for more details.       **
**                                                                        **
**   You should have received a copy of the GNU  Lesser General Public    **
**   License along with Timed. If not, see http://www.gnu.org/licenses/   **
**                                                                        **
***************************************************************************/
#include "adaptor.h"
#include "timed.h"
#include "event.h"

#include "timed/imagetype.h"
#include "timed/log.h"

#include "f.h"

#include <qmlog>

#include <QMetaType>
int main(int ac, char **av)
{
  int syslog_level = qmlog::Full ;
  int varlog_level = qmlog::Full ;

#if F_IMAGE_TYPE
  string image_type = imagetype() ;
  bool debug_flag = access(F_FORCE_DEBUG_PATH, F_OK) == 0 ;
  if (not debug_flag)
  {
    if (image_type=="PR")
      syslog_level = varlog_level = qmlog::None ;
    else if(image_type=="RD")
      syslog_level = qmlog::Notice ;
    else if(image_type=="TR")
      syslog_level = qmlog::Info ;
  }
#endif

  qmlog::syslog()->reduce_max_level(syslog_level) ;

  qmlog::log_file *varlog = new qmlog::log_file("/var/log/timed.log", varlog_level) ;
  varlog->enable_fields(qmlog::Monotonic_Milli | qmlog::Time_Milli) ;

  bool isatty_2 = isatty(2) ;

  if (not isatty_2) // stderr is not a terminal -> no stderr logging
    delete qmlog::stderr() ;

  LIBTIMED_LOGGING_DISPATCHER->set_proxy(qmlog::dispatcher()) ;

  log_notice("time daemon started, debug_flag=%d, syslog-level=%d /var/log-level=%d isatty(2)=%d", debug_flag, qmlog::syslog()->log_level(), varlog->log_level(), isatty_2) ;

#if F_IMAGE_TYPE
  log_notice("image_type='%s'", image_type.c_str()) ;
#endif

  try
  {
    event_t::codec_initializer() ;
    Timed *server = new Timed(ac,av) ;
    int result = server->exec() ;
    string halt = server->is_halted() ;
    if (!halt.empty())
    {
      const char *cud = "clear-device", *rfs = "restore-original-settings" ;
      if(halt==cud || halt==rfs)
      {
        log_info("halt: '%s' requested", halt.c_str()) ;
        const char *rm_all_files = "rm -rf /var/cache/timed/*" ;
        const char *rm_settings = "rm -rf /var/cache/timed/settings*" ;
        const char *cmd = halt==cud ? rm_all_files : rm_settings ;
        int res = system(cmd) ;
        if (res != 0)
          log_critical("'%s' failed with res=%d: %m", cmd, res) ;
        else
          log_info("cache files erased successfully by '%s'", cmd) ;
      }<commit_msg>stderr logging<commit_after>/***************************************************************************
**                                                                        **
**   Copyright (C) 2009-2010 Nokia Corporation.                           **
**                                                                        **
**   Author: <NAME> <<EMAIL>>                    **
**   Author: <NAME> <<EMAIL>>                    **
**   Author: <NAME> <<EMAIL>>                **
**                                                                        **
**     This file is part of Timed                                         **
**                                                                        **
**     Timed is free software; you can redistribute it and/or modify      **
**     it under the terms of the GNU Lesser General Public License        **
**     version 2.1 as published by the Free Software Foundation.          **
**                                                                        **
**     Timed is distributed in the hope that it will be useful, but       **
**     WITHOUT ANY WARRANTY;  without even the implied warranty  of       **
**     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               **
**     See the GNU Lesser General Public License  for more details.       **
**                                                                        **
**   You should have received a copy of the GNU  Lesser General Public    **
**   License along with Timed. If not, see http://www.gnu.org/licenses/   **
**                                                                        **
***************************************************************************/
#include "adaptor.h"
#include "timed.h"
#include "event.h"

#include "timed/imagetype.h"
#include "timed/log.h"

#include "f.h"

#include <qmlog>

#include <QMetaType>
int main(int ac, char **av)
{
  const size_t cwd_len = 16 ;
  char cwd[cwd_len], *p = getcwd(cwd, cwd_len) ;
  bool cwd_is_root = p!=NULL && strcmp(cwd, "/")==0 ;

  int syslog_level = qmlog::Full ;
  int varlog_level = qmlog::Full ;

#if F_IMAGE_TYPE
  string image_type = imagetype() ;
  bool debug_flag = access(F_FORCE_DEBUG_PATH, F_OK) == 0 ;
  if (not debug_flag)
  {
    if (image_type=="PR")
      syslog_level = varlog_level = qmlog::None ;
    else if(image_type=="RD")
      syslog_level = qmlog::Notice ;
    else if(image_type=="TR")
      syslog_level = qmlog::Info ;
  }
#endif

  qmlog::syslog()->reduce_max_level(syslog_level) ;

  const char *log_file = cwd_is_root ? "/var/log/timed.log" : "timed.log" ;
  qmlog::log_file *varlog = new qmlog::log_file(log_file, varlog_level) ;
  varlog->enable_fields(qmlog::Monotonic_Milli | qmlog::Time_Milli) ;

  bool isatty_2 = isatty(2) ;

  if (not isatty_2 or cwd_is_root) // stderr is not a terminal or started by upstart -> no stderr logging
    delete qmlog::stderr() ;

  LIBTIMED_LOGGING_DISPATCHER->set_proxy(qmlog::dispatcher()) ;

  log_notice("time daemon started, debug_flag=%d, syslog-level=%d /var/log-level=%d isatty(2)=%d", debug_flag, qmlog::syslog()->log_level(), varlog->log_level(), isatty_2) ;

#if F_IMAGE_TYPE
  log_notice("image_type='%s'", image_type.c_str()) ;
#endif

  try
  {
    event_t::codec_initializer() ;
    Timed *server = new Timed(ac,av) ;
    int result = server->exec() ;
    string halt = server->is_halted() ;
    if (!halt.empty())
    {
      const char *cud = "clear-device", *rfs = "restore-original-settings" ;
      if(halt==cud || halt==rfs)
      {
        log_info("halt: '%s' requested", halt.c_str()) ;
        const char *rm_all_files = "rm -rf /var/cache/timed/*" ;
        const char *rm_settings = "rm -rf /var/cache/timed/settings*" ;
        const char *cmd = halt==cud ? rm_all_files : rm_settings ;
        int res = system(cmd) ;
        if (res != 0)
          log_critical("'%s' failed with res=%d: %m", cmd, res) ;
        else
          log_info("cache files erased successfully by '%s'", cmd) ;
      }
{'fraction_non_alphanumeric': 0.16145165427032573, 'fraction_numerical': 0.004744806360605283, 'mean_word_length': 1.8216353111432706, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 44, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5961590', 'n_tokens_mistral': 2301, 'n_tokens_neox': 2232, 'n_words': 565}