Class: Yast::LogViewCoreClass

Inherits:
Module
  • Object
show all
Defined in:
library/log/src/modules/LogViewCore.rb

Instance Method Summary collapse

Instance Method Details

#DeleteOldLinesObject

Remove unneeded items from a list of lines If max_lines is 0, then don't remove anything



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'library/log/src/modules/LogViewCore.rb', line 85

def DeleteOldLines
  max_lines = Ops.get_integer(@data, "max_lines", @max_lines_default)
  return if max_lines == 0

  if Ops.greater_than(
    Ops.subtract(Ops.subtract(Builtins.size(@lines), max_lines), 1),
    0
  )
    @lines = Builtins.sublist(
      @lines,
      Ops.subtract(Ops.subtract(Builtins.size(@lines), max_lines), 1)
    )
  end

  nil
end

#GetLinesObject



224
225
226
# File 'library/log/src/modules/LogViewCore.rb', line 224

def GetLines
  deep_copy(@lines)
end

#GetNewLinesObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'library/log/src/modules/LogViewCore.rb', line 62

def GetNewLines
  return [] if !@is_running

  if !Convert.to_boolean(SCR.Read(path(".process.running"), @id))
    @is_running = false
    Report.Error(_("Error occurred while reading the log."))
    return []
  end

  new_lines = []

  loop do
    line = Convert.to_string(SCR.Read(path(".process.read_line"), @id))
    break if line.nil?

    new_lines = Builtins.add(new_lines, line)
  end

  deep_copy(new_lines)
end

#mainObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'library/log/src/modules/LogViewCore.rb', line 35

def main
  Yast.import "UI"

  textdomain "base"

  Yast.import "Report"

  # default value of maximum displayed lines
  @max_lines_default = 100

  # lines of the log
  @lines = []

  # data describing log:
  #   file:       filename to read from
  #   grep:       grep file with expression
  #   command:    command to run (use instead of file and grep)
  #   max_lines:  max lines to keep (0 -> infinite)
  @data = {}

  # id of background process
  @id = nil

  # flag indicating if background process is (or should be) running
  @is_running = false
end

#Start(widget, d) ⇒ Object

Starts the log reading command via process agent.

The LogView widget must exist when calling this function. The `MaxLines parameter of the widget will be set.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'library/log/src/modules/LogViewCore.rb', line 106

def Start(widget, d)
  widget = deep_copy(widget)
  d = deep_copy(d)
  if !@id.nil?
    SCR.Execute(path(".process.release"), @id)
    @id = nil
  end

  @data = deep_copy(d)

  file = Ops.get_string(@data, "file", "")
  grep = Ops.get_string(@data, "grep", "")
  command = Ops.get_string(@data, "command", "")
  max_lines = Ops.get_integer(@data, "max_lines", @max_lines_default)

  if command == ""
    if grep == ""
      command = Builtins.sformat("tail -n %1 -f '%2'", max_lines, file)
    else
      command = Builtins.sformat(
        "tail -n +0 -f '%1' | grep --line-buffered '%2'",
        file,
        grep
      )

      if max_lines != 0
        lc_command = Builtins.sformat(
          "cat '%1' | grep '%2' | wc -l",
          file,
          grep
        )
        bash_output = Convert.to_map(
          SCR.Execute(path(".target.bash_output"), lc_command)
        )
        if Ops.get_integer(bash_output, "exit", 1) == 0
          lc = Builtins.filterchars(
            Ops.get_string(bash_output, "stdout", ""),
            "1234567890"
          )
          lines_count = Builtins.tointeger(lc)

          # don't know why without doubling it discards more lines,
          # out of YaST2 it works
          lines_count = Ops.subtract(
            lines_count,
            Ops.multiply(2, max_lines)
          )
          lines_count = 0 if Ops.less_than(lines_count, 0)

          if Ops.greater_than(lines_count, 0)
            command = Ops.add(
              command,
              Builtins.sformat(" | tail -n +%1", lines_count)
            )
          end
        end
      end
    end
  end

  Builtins.y2milestone("Calling process agent with command %1", command)

  @id = Convert.to_integer(
    SCR.Execute(path(".process.start_shell"), command, "tty" => true)
  )
  @is_running = true

  Builtins.sleep(100)

  @lines = GetNewLines()
  DeleteOldLines()

  UI.ChangeWidget(widget, :MaxLines, max_lines)
  UI.ChangeWidget(
    widget,
    :Value,
    Builtins.mergestring(Builtins.maplist(@lines) do |line|
      Ops.add(line, "\n")
    end, "")
  )

  nil
end

#StopObject



215
216
217
218
219
220
221
222
# File 'library/log/src/modules/LogViewCore.rb', line 215

def Stop
  if !@id.nil?
    SCR.Execute(path(".process.release"), @id)
    @id = nil
  end

  nil
end

#Update(widget) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'library/log/src/modules/LogViewCore.rb', line 190

def Update(widget)
  widget = deep_copy(widget)
  if !@id.nil?
    new_lines = GetNewLines()
    if Ops.greater_than(Builtins.size(new_lines), 0)
      @lines = Convert.convert(
        Builtins.merge(@lines, new_lines),
        from: "list",
        to:   "list <string>"
      )
      DeleteOldLines()

      UI.ChangeWidget(
        widget,
        :LastLine,
        Builtins.mergestring(Builtins.maplist(new_lines) do |line|
          Ops.add(line, "\n")
        end, "")
      )
    end
  end

  nil
end