<commit_before>$LOAD_PATH << File.dirname(__FILE__)

require 'rack'
require 'blank'

require 'stringify_hash'

require 'template'
require 'route'
require 'session'
require 'helper'

require 'time'
require 'monitor'
require 'digest/md5'

module Rack
  module Utils
    def normalize_params(params,name,v)
      params[name] = v
    end
    module_function :normalize_params
  end
end

class CriticalError < RuntimeError
end

module Egalite

  module AccessLogger
   @@io = nil
   @@time = nil
   @@lock = Monitor.new
   class <<self
    def io=(io)
      @@io=io
    end
    def _open
      @@dir = dir
      @@time = Time.now
      fn = sprintf("egaliteaccess-%04d-%02d-%02d-p%d.log", @@time.year, @@time.month, @@time.mday, Process.pid)
      @@io = open(File.join(dir,fn), "a")
    end
    def open(dir)
      @@dir = dir
      yield
    ensure
      @@io.close if @@io
    end
    def write(line)
      return nil unless @@io
      
      @@lock.synchronize {
        if @@time and (@@time.mday != Time.now.mday)
          ## log rotation
          @@io.close
          _open
        end
        @@io.puts(line)
      }
    end
   end
  end
  
  class DebugLogger
    def initialize(path)
      @path = path
    end
    def puts(s)
      open(@path, "a") { |f|
        begin
          f.flock(File::LOCK_EX)
          f.puts s
          f.flush
        ensure
          f.flock(File::LOCK_UN)
        end
      }
    end
  end
  
  module ErrorLogger
   @@table = nil
   @@admin_emails = nil
   class <<self
    def table=(t)
      @@table=t
    end
    def admin_emails=(t)
      @@admin_emails=t
    end
    def write(hash)
      hash[:md5] = Digest::MD5.hexdigest(hash[:text]) unless hash[:md5]
      if hash[:severity] == 'critical' and @@admin_emails
        Sendmail.send(hash[:text],{
          :from => '<EMAIL>',
          :to => @@admin_emails,
          :subject => 'Critical error at xcream.net'
        })
      end
      @@table << hash if @@table
    end
    def write_exception(e, hash)
      severity = 'exception'<commit_msg>changed from '<<' to 'insert'.because Dataset#<< now return self.<commit_after>$LOAD_PATH << File.dirname(__FILE__)

require 'rack'
require 'blank'

require 'stringify_hash'

require 'template'
require 'route'
require 'session'
require 'helper'

require 'time'
require 'monitor'
require 'digest/md5'

module Rack
  module Utils
    def normalize_params(params,name,v)
      params[name] = v
    end
    module_function :normalize_params
  end
end

class CriticalError < RuntimeError
end

module Egalite

  module AccessLogger
   @@io = nil
   @@time = nil
   @@lock = Monitor.new
   class <<self
    def io=(io)
      @@io=io
    end
    def _open
      @@dir = dir
      @@time = Time.now
      fn = sprintf("egaliteaccess-%04d-%02d-%02d-p%d.log", @@time.year, @@time.month, @@time.mday, Process.pid)
      @@io = open(File.join(dir,fn), "a")
    end
    def open(dir)
      @@dir = dir
      yield
    ensure
      @@io.close if @@io
    end
    def write(line)
      return nil unless @@io
      
      @@lock.synchronize {
        if @@time and (@@time.mday != Time.now.mday)
          ## log rotation
          @@io.close
          _open
        end
        @@io.puts(line)
      }
    end
   end
  end
  
  class DebugLogger
    def initialize(path)
      @path = path
    end
    def puts(s)
      open(@path, "a") { |f|
        begin
          f.flock(File::LOCK_EX)
          f.puts s
          f.flush
        ensure
          f.flock(File::LOCK_UN)
        end
      }
    end
  end
  
  module ErrorLogger
   @@table = nil
   @@admin_emails = nil
   class <<self
    def table=(t)
      @@table=t
    end
    def admin_emails=(t)
      @@admin_emails=t
    end
    def write(hash)
      hash[:md5] = Digest::MD5.hexdigest(hash[:text]) unless hash[:md5]
      if hash[:severity] == 'critical' and @@admin_emails
        Sendmail.send(hash[:text],{
          :from => '<EMAIL>',
          :to => @@admin_emails,
          :subject => 'Critical error at xcream.net'
        })
      end
      @@table.insert(hash) if @@table
    end
    def write_exception(e, hash)
      severity = 'exception'
{'fraction_non_alphanumeric': 0.1277416244878284, 'fraction_numerical': 0.004820438659918052, 'mean_word_length': 2.055964653902798, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 11, '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': '850863', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1401, 'n_words': 349}