Class: Yast2::LogDirRotator

Inherits:
Object
  • Object
show all
Includes:
Yast::Logger
Defined in:
library/general/src/lib/yast2/log_dir_rotator.rb

Overview

The class LogDirRotator handels creating, rotating and removing directories for logging in the YaST2 log directory according to this schema:

rm #NAME-03 mv #NAME-02 #NAME-03 mv #NAME-01 #NAME-02 mv #NAME #NAME-01

System error are logged and no exception is raised by the functions.

In case Mode.test is set the fuction do not perform any work. Otherwise legacy testsuites may fail.

TODO This class is based on dump_manager.rb and could be used there.

Direct Known Subclasses

ControlLogDirRotator

Constant Summary collapse

KEEP_OLD_LOG_DIRS =

The number of old log directories to keep. This is in addition to the current one. Child classes can override the value. Max value is 99.

3
NAME =

The name of the directory in /var/log/YaST2 to create, rotate and delete. Child classes should override the value.

"undefined".freeze

Instance Method Summary collapse

Instance Method Details

#copy(src, dest) ⇒ Object

Copy a file to the log directory.

Parameters:

  • src (String)

    name of source

  • dest (String)

    basename of destination



74
75
76
77
78
79
80
81
82
# File 'library/general/src/lib/yast2/log_dir_rotator.rb', line 74

def copy(src, dest)
  return if Yast::Mode.test

  begin
    FileUtils.cp(src, log_dir + "/" + dest)
  rescue SystemCallError => e
    log.error e.to_s
  end
end

#log_dirString

Return a suitable name for the log directory depending on the YaST mode (installation / installed system).

Returns:

  • (String)

    directory name with full path



102
103
104
105
# File 'library/general/src/lib/yast2/log_dir_rotator.rb', line 102

def log_dir
  dir = installation? ? "#{self.class::NAME}-inst" : self.class::NAME
  base_dir + "/" + dir
end

#prepareObject

Prepare (create, rotate and delete) the log directory.



58
59
60
61
62
63
64
65
66
67
68
# File 'library/general/src/lib/yast2/log_dir_rotator.rb', line 58

def prepare
  return if Yast::Mode.test

  begin
    log.info "preparing log dir #{self.class::NAME}"
    rotate_log_dirs
    FileUtils.mkdir_p(log_dir)
  rescue SystemCallError => e
    log.error e.to_s
  end
end

#write(dest, content) ⇒ Object

Write a file in the log directory.

Parameters:

  • dest (String)

    basename of destination

  • content (String)

    content to write



88
89
90
91
92
93
94
95
96
# File 'library/general/src/lib/yast2/log_dir_rotator.rb', line 88

def write(dest, content)
  return if Yast::Mode.test

  begin
    File.write(log_dir + "/" + dest, content)
  rescue SystemCallError => e
    log.error e.to_s
  end
end