<commit_before>local function logJsonRecursive(obj)
  if type(obj) == 'string' then
    io.write('"' .. obj .. '"')
  elseif type(obj) == 'table' then
    local first = true

    io.write('{')

    for key, val in pairs(obj) do
      if not first then
        io.write(',')
      else
        first = false
      end
      io.write('"' .. key .. '":')
      logJsonRecursive(val)
    end

    io.write('}')
  else
    io.write(tostring(obj))
  end
end

--[[ Recursively outputs a Lua object to a JSON objects followed by a new line. ]]
local function logJson(obj)
  logJsonRecursive(obj)
  io.write('\n')
end

return {
  logJson = logJson
}<commit_msg>Update Log.lua<commit_after>require 'class'
require 'paths'

local function logJsonRecursive(obj)
  if type(obj) == 'string' then
    io.write('"' .. obj .. '"')
  elseif type(obj) == 'table' then
    local first = true

    io.write('{')

    for key, val in pairs(obj) do
      if not first then
        io.write(',')
      else
        first = false
      end
      io.write('"' .. key .. '":')
      logJsonRecursive(val)
    end

    io.write('}')
  else
    io.write(tostring(obj))
  end
end

--[[ Recursively outputs a Lua object to a JSON objects followed by a new line. ]]
local function logJson(obj)
  logJsonRecursive(obj)
  io.write('\n')
end


--[[ logging is a class used for maintaining logs in a log file.
--]]
local logging = torch.class('onmt.logging')

--[[ Construct a logging object.
Parameters:
  * `logPath` - the path to log file.
  * `mute` - whether or not outputs to screen.
]]
function logging:__init(logPath, mute)
  mute = mute or false
  self.mute = mute
  local openMode = 'w'
  if paths.filep(logPath) then
    local input = nil
    while not input do
      print('Logging file exits. Overwrite(o)? Append(a)? Abort(q)?')
      input = io.read()
      if input == 'o' or input == 'O' then
        openMode = 'w'
      elseif input == 'a' or input == 'A' then
        openMode = 'a'
      elseif input == 'q' or input == 'Q' then
        os.exit()
      else
        openMode = 'a'
      end
    end
  end
  self.logFile = io.open(logPath, openMode)
end

--[[ Log a message at a specified level.
Parameters:
  * `message` - the message to log.
  * `level` - the desired message level. ['INFO']
]]
function logging:log(message, level)
  level = level or 'INFO'
  local timeStamp = os.date('%x %X')
  local msgFormatted = string.format('[%s %s] %s', timeStamp, level, message)
  if not self.mute then
    print (msgFormatted)
  end
  if self.logFile then
    self.logFile:write(msgFormatted .. '\n')
    self.logFile:flush()
  end
end

--[[ Log a message at 'INFO' level.
Parameters:
  * `message` - the message to log. Supports formatting string.
]]
function logging:info(...)
  self:log(string.format(...), 'INFO')
end

--[[ Log a message at 'WARNING' level.
Parameters:
  * `message` - the message to log. Supports formatting string.
]]
function logging:warning(...)
  self:log(string.format(...), 'WARNING')
end

--[[ Log a message at 'ERROR' level.
Parameters:
  * `message` - the message to log. Supports formatting string.
]]
function logging:error(...)
  self:log(string.format(...), 'ERROR')
end

--[[ Deconstructor. Close the log file.
]]
function logging:shutDown()
  if self.logFile then
    self.logFile:close()
  end
end

return {
  logJson = logJson
}
{'fraction_non_alphanumeric': 0.1454599761051374, 'fraction_numerical': 0.0, 'mean_word_length': 2.70874861572536, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '6568137', 'n_tokens_mistral': 1142, 'n_tokens_neox': 1128, 'n_words': 358}