File: loggers.cc

package info (click to toggle)
gddrescue 1.23-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 1,052 kB
  • sloc: cpp: 4,961; sh: 669; makefile: 150
file content (197 lines) | stat: -rw-r--r-- 5,343 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
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
189
190
191
192
193
194
195
196
197
/*  GNU ddrescue - Data recovery tool
    Copyright (C) 2013-2018 Antonio Diaz Diaz.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#define _FILE_OFFSET_BITS 64

#include <cstdio>
#include <string>
#include <vector>
#include <sys/stat.h>

#include "block.h"
#include "loggers.h"


namespace {

const char * format_time_dhms( const long t )
  {
  static char buf[32];
  const int s = t % 60;
  const int m = ( t / 60 ) % 60;
  const int h = ( t / 3600 ) % 24;
  const long d = t / 86400;

  if( d ) snprintf( buf, sizeof buf, "%ldd:%02dh:%02dm:%02ds", d, h, m, s );
  else if( h ) snprintf( buf, sizeof buf, "%dh:%02dm:%02ds", h, m, s );
  else if( m ) snprintf( buf, sizeof buf, "%dm:%02ds", m, s );
  else snprintf( buf, sizeof buf, "%ds", s );
  return buf;
  }

} // end namespace


Event_logger event_logger;
Rate_logger rate_logger;
Read_logger read_logger;


bool Logger::set_filename( const char * const name )
  {
  if( name && name[0] )
    {
    struct stat st;
    if( stat( name, &st ) == 0 && !S_ISREG( st.st_mode ) ) return false;
    filename_ = name;
    }
  return true;
  }


bool Logger::close_file()
  {
  if( f && !error && !write_final_timestamp( f ) ) error = true;
  if( f && std::fclose( f ) != 0 ) error = true;
  f = 0;
  return !error;
  }


bool Event_logger::open_file()
  {
  if( !filename_ ) return true;
  if( !f )
    {
    struct stat st;
    const bool file_exists = ( stat( filename_, &st ) == 0 );
    f = std::fopen( filename_, "a" );
    error = !f || ( file_exists && std::fputc( '\n', f ) == EOF ) ||
            !write_file_header( f, "Events Logfile" ) ||
            std::fputs( "#         Time  Rescued  Event\n", f ) == EOF;
    }
  return !error;
  }


bool Event_logger::echo_msg( const char * const msg )
  {
  if( verbosity >= 0 ) std::printf( "\n  %s", msg );
  if( f && !error && std::fprintf( f, "                      %s\n", msg ) < 0 )
    error = true;
  return !error;
  }


bool Event_logger::print_msg( const long time, const char * const percent_rescued,
                              const char * const msg )
  {
  if( f && !error &&
      std::fprintf( f, "%14s  %s  %s\n", format_time_dhms( time ),
                    percent_rescued, msg ) < 0 )
    error = true;
  return !error;
  }


bool Event_logger::print_eor( const long time, const char * const percent_rescued,
                              const long long current_pos,
                              const char * const current_status_name )
  {
  if( f && !error &&
      std::fprintf( f, "%14s  %s  End of run (0x%08llX  %s)\n",
                    format_time_dhms( time ), percent_rescued,
                    current_pos, current_status_name ) < 0 )
    error = true;
  return !error;
  }


bool Rate_logger::open_file()
  {
  if( !filename_ ) return true;
  if( !f )
    {
    last_time = -1;
    f = std::fopen( filename_, "w" );
    error = !f || !write_file_header( f, "Rates Logfile" ) ||
            std::fputs( "#Time  Ipos  Current_rate  Average_rate  Bad_areas  Bad_size\n", f ) == EOF;
    }
  return !error;
  }


bool Rate_logger::print_line( const long time, const long long ipos,
                              const long long a_rate, const long long c_rate,
                              const unsigned long bad_areas,
                              const long long bad_size )
  {
  if( f && !error && time > last_time )
    {
    last_time = time;
    if( std::fprintf( f, "%2ld  0x%08llX  %8lld  %8lld  %7lu  %8lld\n",
                      time, ipos, c_rate, a_rate, bad_areas, bad_size ) < 0 )
      error = true;
    }
  return !error;
  }


bool Read_logger::open_file()
  {
  if( !filename_ ) return true;
  if( !f )
    {
    prev_is_msg = true;
    f = std::fopen( filename_, "w" );
    error = !f || !write_file_header( f, "Reads Logfile" ) ||
            std::fputs( "#  Ipos       Size  Copied_size  Error_size\n", f ) == EOF;
    }
  return !error;
  }


bool Read_logger::print_line( const long long ipos, const long long size,
                              const int copied_size, const int error_size )
  {
  if( f && !error &&
      std::fprintf( f, "0x%08llX	%lld	%d	%d\n",
                    ipos, size, copied_size, error_size ) < 0 )
    error = true;
  prev_is_msg = false;
  return !error;
  }


bool Read_logger::print_msg( const long time, const char * const msg )
  {
  if( f && !error &&
      std::fprintf( f, "%s# %s  %s\n", prev_is_msg ? "" : "\n",
                    format_time_dhms( time ), msg ) < 0 )
    error = true;
  prev_is_msg = true;
  return !error;
  }


bool Read_logger::print_time( const long time )
  {
  if( f && !error && time > 0 &&
      std::fprintf( f, "# %s\n", format_time_dhms( time ) ) < 0 )
    error = true;
  return !error;
  }