Class: Baykit::BayServer::BayLog

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/baykit/bayserver/bay_log.rb

Constant Summary collapse

LOG_LEVEL_TRACE =
0
LOG_LEVEL_DEBUG =
1
LOG_LEVEL_INFO =
2
LOG_LEVEL_WARN =
3
LOG_LEVEL_ERROR =
4
LOG_LEVEL_FATAL =
5
LOG_LEVEL_NAME =
["TRACE", "DEBUG", "INFO ", "WARN ", "ERROR", "FATAL"]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.full_pathObject (readonly)

Returns the value of attribute full_path.



19
20
21
# File 'lib/baykit/bayserver/bay_log.rb', line 19

def full_path
  @full_path
end

.log_levelObject (readonly)

Returns the value of attribute log_level.



18
19
20
# File 'lib/baykit/bayserver/bay_log.rb', line 18

def log_level
  @log_level
end

Class Method Details

.debug(fmt, *args) ⇒ Object



50
51
52
# File 'lib/baykit/bayserver/bay_log.rb', line 50

def self.debug(fmt, *args)
  log(LOG_LEVEL_DEBUG, 3, nil, fmt, args)
end

.debug_e(err, fmt = nil, *args) ⇒ Object



54
55
56
# File 'lib/baykit/bayserver/bay_log.rb', line 54

def self.debug_e(err, fmt=nil, *args)
  log(LOG_LEVEL_DEBUG, 3, err, fmt, args)
end

.debug_mode?Boolean

Returns:

  • (Boolean)


124
125
126
127
# File 'lib/baykit/bayserver/bay_log.rb', line 124

def self.debug_mode?
  @log_level <= LOG_LEVEL_DEBUG
  #LOG_LEVEL_TRACE < LOG_LEVEL_INFO
end

.error(fmt, *args) ⇒ Object



66
67
68
# File 'lib/baykit/bayserver/bay_log.rb', line 66

def self.error(fmt, *args)
  log(LOG_LEVEL_ERROR, 3, nil , fmt, args)
end

.error_e(err, fmt = nil, *args) ⇒ Object



70
71
72
# File 'lib/baykit/bayserver/bay_log.rb', line 70

def self.error_e(err, fmt=nil, *args)
  log(LOG_LEVEL_ERROR, 3, err, fmt, args)
end

.fatal(fmt, *args) ⇒ Object



74
75
76
# File 'lib/baykit/bayserver/bay_log.rb', line 74

def self.fatal(fmt, *args)
  log(LOG_LEVEL_FATAL, 3, nil , fmt, args)
end

.fatal_e(err, fmt = nil, *args) ⇒ Object



78
79
80
81
# File 'lib/baykit/bayserver/bay_log.rb', line 78

def self.fatal_e(err, fmt=nil, *args)
  log(LOG_LEVEL_FATAL, 3, err, fmt, args)
  exit(1)
end

.info(fmt, *args) ⇒ Object



42
43
44
# File 'lib/baykit/bayserver/bay_log.rb', line 42

def self.info(fmt, *args)
  log(LOG_LEVEL_INFO, 3, nil, fmt, args)
end

.log(lvl, stack_idx, err, fmt, args) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/baykit/bayserver/bay_log.rb', line 83

def self.log(lvl, stack_idx, err, fmt, args)
  if lvl < @log_level
    return
  end

  #pos = caller[1].split("/")[-1]
  apos = parse_caller(caller[1])
  if(!@full_path)
    apos[0] = File.basename(apos[0])
  end
  pos = "#{apos[0]}:#{apos[1]}"
  #pos = caller[1]

  if fmt != nil
    begin
      if args == nil || args.length == 0
        msg = sprintf("%s", fmt)
      else
        msg = sprintf(fmt, *args)
      end
    rescue => e
      puts(e.class)
      puts(e.message + " " + pos)
      print_exception(e)
      msg = fmt
    end

    print("[#{Time.now}] #{LOG_LEVEL_NAME[lvl]}. #{msg} (at #{pos})\n")
  end

  if err != nil
    if debug_mode? || lvl == LOG_LEVEL_FATAL
      puts(err.class)
      puts(err.message + " " + pos)
      print_exception err
    else
      log(lvl, stack_idx + 1, nil, "%s", err.message)
    end
  end
end

.parse_caller(str) ⇒ Object



147
148
149
150
# File 'lib/baykit/bayserver/bay_log.rb', line 147

def self.parse_caller(str)
  m = /(.*):(.*):in `(.*)'/.match(str)
  return [m[1], m[2], m[3]]
end


133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/baykit/bayserver/bay_log.rb', line 133

def self.print_exception err
  if err.backtrace != nil
    for s in err.backtrace
      puts "\t" + s
    end
  end
  if err.cause
    puts "Caused by:"
    puts err.cause.message
    print_exception err.cause
  end
end

.set_log_level(lvl) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/baykit/bayserver/bay_log.rb', line 24

def self.set_log_level(lvl)
  if lvl.casecmp? "trace"
    @log_level = LOG_LEVEL_TRACE
  elsif lvl.casecmp? "debug"
    @log_level = LOG_LEVEL_DEBUG
  elsif lvl.casecmp? "info"
    @log_level = LOG_LEVEL_INFO
  elsif lvl.casecmp? "warn"
    @log_level = LOG_LEVEL_WARN
  elsif lvl.casecmp? "error"
    @log_level = LOG_LEVEL_ERROR
  elsif lvl.casecmp? "fatal"
    @log_level = LOG_LEVEL_FATAL
  else
    warn(BayMessage.get(:INT_UNKNOWN_LOG_LEVEL, lvl))
  end
end

.trace(fmt, *args) ⇒ Object



46
47
48
# File 'lib/baykit/bayserver/bay_log.rb', line 46

def self.trace(fmt, *args)
  log(LOG_LEVEL_TRACE, 3, nil , fmt, args)
end

.trace_mode?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/baykit/bayserver/bay_log.rb', line 129

def self.trace_mode?
  @log_level == LOG_LEVEL_TRACE
end

.warn(fmt, *args) ⇒ Object



58
59
60
# File 'lib/baykit/bayserver/bay_log.rb', line 58

def self.warn(fmt, *args)
  log(LOG_LEVEL_WARN, 3, nil, fmt, args)
end

.warn_e(err, fmt = nil, *args) ⇒ Object



62
63
64
# File 'lib/baykit/bayserver/bay_log.rb', line 62

def self.warn_e(err, fmt=nil, *args)
  log(LOG_LEVEL_WARN, 3, err, fmt, args)
end