Class: GTK::Log

Inherits:
Object show all
Defined in:
dragon/log.rb

Class Method Summary collapse

Class Method Details

.dynamic_blockObject


76
77
78
79
80
81
82
# File 'dragon/log.rb', line 76

def self.dynamic_block
  "#+BEGIN:
#{join_lines yield}
#+END:

"
end

.headline(name) ⇒ Object


65
66
67
68
69
70
71
72
73
74
# File 'dragon/log.rb', line 65

def self.headline name
  @asterisk_count ||= 1
  @asterisk_count = @asterisk_count.greater(1)
  result_from_yield = join_lines yield
  result_from_yield = result_from_yield.each_line.map { |l| "  #{l}" }.join
  r ="#{"*" * @asterisk_count} #{name}\n#{result_from_yield}"
  @asterisk_count -= 1
  @asterisk_count = @asterisk_count.greater(1)
  r
end

.join_lines(args) ⇒ Object


58
59
60
61
62
63
# File 'dragon/log.rb', line 58

def self.join_lines args
  return "" if args.length == 0
  return args if args.is_a? String
  return args[0] if args.length == 1
  return args.to_s.join("\n")
end

.multiline?(*args) ⇒ Boolean

Returns:

  • (Boolean)

53
54
55
56
# File 'dragon/log.rb', line 53

def self.multiline? *args
  return true if args.length > 1
  return !args[0].to_s.multiline?
end

155
156
157
# File 'dragon/log.rb', line 155

def self.print *args
  write_to_log_and_print(*args)
end

.puts(*args) ⇒ Object


47
48
49
50
51
# File 'dragon/log.rb', line 47

def self.puts *args
  message_id, message = args
  message ||= message_id
  write_to_log_and_puts message
end

.puts_error(*args) ⇒ Object


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'dragon/log.rb', line 84

def self.puts_error *args
  args ||= []
  title = args[0]
  additional = args[1..-1] || []
  additional = "" if additional.length == 0
  if !title.multiline? && join_lines(additional).multiline?
    message = headline "ERROR: #{title}" do
      dynamic_block do
        additional
      end
    end
  elsif title.multiline?
    message = headline "ERROR: " do
      dynamic_block do
        args
      end
    end
  else
    message = "* ERROR: #{title} #{additional}".strip
  end

  self.puts message
end

.puts_important(*args) ⇒ Object


40
41
42
43
44
45
# File 'dragon/log.rb', line 40

def self.puts_important *args
  return if $gtk.production
  $gtk.append_file_root 'logs/log.txt', args.join("\n")
  $gtk.notify! "Important notification occurred."
  args.each { |obj| $gtk.log obj }
end

.puts_info(*args) ⇒ Object


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'dragon/log.rb', line 108

def self.puts_info *args
  args ||= []
  title = args[0]
  additional = args[1..-1] || []
  additional = "" if additional.length == 0
  if !title.multiline? && join_lines(additional).multiline?
    message = headline "INFO: #{title}" do
      dynamic_block do
        additional
      end
    end
  elsif title.multiline?
    message = headline "INFO: " do
      dynamic_block do
        args
      end
    end
  else
    message = "* INFO: #{title} #{additional}".strip
  end

  self.puts message
end

.puts_once(*ids, message) ⇒ Object


132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'dragon/log.rb', line 132

def self.puts_once *ids, message
  id = "#{ids}"
  @once ||= {}
  return if @once[id]
  @once[id] = id
  if !$gtk.cli_arguments[:replay] && !$gtk.cli_arguments[:record]
    $gtk.notify!("Open the DragonRuby Console by pressing [`] [~] [²] [^] [º] or [§]. [Message ID: #{id}].")
  end
  write_to_log_and_puts ""
  write_to_log_and_puts "#{message.strip}"
  write_to_log_and_puts ""
  write_to_log_and_puts "[Message ID: #{id}]"
  write_to_log_and_puts ""
end

.puts_once_info(*ids, message) ⇒ Object


147
148
149
150
151
152
153
# File 'dragon/log.rb', line 147

def self.puts_once_info *ids, message
  id = "#{ids}"
  @once ||= {}
  return if @once[id]
  @once[id] = id
  log_info message
end

.write_to_log_and_print(*args) ⇒ Object


34
35
36
37
38
# File 'dragon/log.rb', line 34

def self.write_to_log_and_print *args
  return if $gtk.production
  $gtk.append_file_root 'logs/log.txt', args.join("\n")
  Object.print(*args)
end

.write_to_log_and_puts(*args) ⇒ Object


28
29
30
31
32
# File 'dragon/log.rb', line 28

def self.write_to_log_and_puts *args
  return if $gtk.production
  $gtk.append_file_root 'logs/log.txt', args.join("\n") + "\n"
  args.each { |obj| $gtk.log obj, self }
end