Pascal · projectA log viewer with colours and search in Free Pascal
This program lets you read a long log in the SSH window: 21 lines at a time with their numbers, warnings in amber, errors in red, and a jump to the next error with one key. A status line shows which lines you see and how far down the file you are.
viewer.log 83 lines 1 2026-11-21 00:00:02 INFO [System] Log rotation done, 3 files kept 2 2026-11-21 02:00:01 INFO [Backup] Job "Daily to USB" started 3 2026-11-21 02:39:16 INFO [Backup] Job "Daily to USB" finished, 1768 fi› 4 2026-11-21 03:00:00 INFO [Snapshot] Snapshot of DataVol1 taken, 30 kept 5 2026-11-21 06:34:06 INFO [Users] admin signed in from 192.0.2.51 (SSH) 6 2026-11-21 09:15:00 INFO [Users] admin signed in from 192.0.2.51 (SSH) 7 2026-11-21 13:13:26 INFO [App Center] Hybrid Backup Sync updated 8 2026-11-21 16:11:58 WARN [Hardware] Disk 2 temperature 50 C 9 2026-11-21 18:10:48 INFO [App Center] TurboShell updated 10 2026-11-21 19:08:39 INFO [Users] admin signed in from 192.0.2.51 (SSH) 11 2026-11-21 22:28:08 INFO [Storage] RAID scrubbing of md1 at 26 % 12 2026-11-21 23:30:00 INFO [System] Firmware check: QTS is up to date 13 2026-11-22 00:00:02 INFO [System] Log rotation done, 3 files kept 14 2026-11-22 02:00:01 INFO [Backup] Job "Daily to USB" started 15 2026-11-22 02:20:55 INFO [Backup] Job "Daily to USB" finished, 321 fil› 16 2026-11-22 03:00:00 INFO [Snapshot] Snapshot of DataVol1 taken, 23 kept 17 2026-11-22 08:23:26 INFO [App Center] Container Station updated 18 2026-11-22 09:59:09 INFO [Users] admin signed in from 192.0.2.24 (SSH) 19 2026-11-22 10:04:21 INFO [Users] admin signed in from 192.0.2.24 (SSH) 20 2026-11-22 11:52:38 INFO [Storage] RAID scrubbing of md1 at 85 % 21 2026-11-22 12:00:38 INFO [App Center] Hybrid Backup Sync updated Lines 1-21 of 83 25 % ↑ ↓ PgUp PgDn Home End / next error q quit
viewer.log 83 lines 22 2026-11-22 19:04:19 INFO [Shares] Folder "Multimedia" scanned, 48 new › 23 2026-11-22 21:52:19 INFO [Users] admin signed in from 192.0.2.24 (SSH) 24 2026-11-22 23:30:00 INFO [System] Firmware check: QTS is up to date 25 2026-11-23 00:00:02 INFO [System] Log rotation done, 3 files kept 26 2026-11-23 02:00:01 INFO [Backup] Job "Daily to USB" started 27 2026-11-23 02:41:17 ERROR [Backup] Job "Daily to USB" stopped: no free › 28 2026-11-23 02:41:17 WARN [Storage] USB disk "Backup2" has less than 5 › 29 2026-11-23 03:00:00 INFO [Snapshot] Snapshot of DataVol1 taken, 25 kept 30 2026-11-23 06:23:24 INFO [Users] admin signed in from 192.0.2.51 (SSH) 31 2026-11-23 08:53:00 INFO [Users] admin signed in from 192.0.2.24 (SSH) 32 2026-11-23 10:02:45 INFO [Hardware] Disk 2 temperature 46 C 33 2026-11-23 11:39:12 INFO [App Center] TurboShell updated 34 2026-11-23 13:48:15 INFO [App Center] Multimedia Console updated 35 2026-11-23 17:22:32 INFO [Storage] RAID scrubbing of md1 at 55 % 36 2026-11-23 21:57:33 INFO [Users] admin signed in from 192.0.2.24 (SSH) 37 2026-11-23 23:30:00 INFO [System] Firmware check: QTS is up to date 38 2026-11-24 00:00:02 INFO [System] Log rotation done, 3 files kept 39 2026-11-24 02:00:01 INFO [Backup] Job "Daily to USB" started 40 2026-11-24 02:44:29 INFO [Backup] Job "Daily to USB" finished, 742 fil› 41 2026-11-24 03:00:00 INFO [Snapshot] Snapshot of DataVol1 taken, 29 kept 42 2026-11-24 06:19:11 INFO [Users] admin signed in from 192.0.2.10 (SSH) Lines 22-42 of 83 50 % ↑ ↓ PgUp PgDn Home End / next error q quit
viewer.log 83 lines 24 2026-11-22 23:30:00 INFO [System] Firmware check: QTS is up to date 25 2026-11-23 00:00:02 INFO [System] Log rotation done, 3 files kept 26 2026-11-23 02:00:01 INFO [Backup] Job "Daily to USB" started 27 2026-11-23 02:41:17 ERROR [Backup] Job "Daily to USB" stopped: no free › 28 2026-11-23 02:41:17 WARN [Storage] USB disk "Backup2" has less than 5 › 29 2026-11-23 03:00:00 INFO [Snapshot] Snapshot of DataVol1 taken, 25 kept 30 2026-11-23 06:23:24 INFO [Users] admin signed in from 192.0.2.51 (SSH) 31 2026-11-23 08:53:00 INFO [Users] admin signed in from 192.0.2.24 (SSH) 32 2026-11-23 10:02:45 INFO [Hardware] Disk 2 temperature 46 C 33 2026-11-23 11:39:12 INFO [App Center] TurboShell updated 34 2026-11-23 13:48:15 INFO [App Center] Multimedia Console updated 35 2026-11-23 17:22:32 INFO [Storage] RAID scrubbing of md1 at 55 % 36 2026-11-23 21:57:33 INFO [Users] admin signed in from 192.0.2.24 (SSH) 37 2026-11-23 23:30:00 INFO [System] Firmware check: QTS is up to date 38 2026-11-24 00:00:02 INFO [System] Log rotation done, 3 files kept 39 2026-11-24 02:00:01 INFO [Backup] Job "Daily to USB" started 40 2026-11-24 02:44:29 INFO [Backup] Job "Daily to USB" finished, 742 fil› 41 2026-11-24 03:00:00 INFO [Snapshot] Snapshot of DataVol1 taken, 29 kept 42 2026-11-24 06:19:11 INFO [Users] admin signed in from 192.0.2.10 (SSH) 43 2026-11-24 07:40:09 INFO [Users] admin signed in from 192.0.2.51 (SSH) 44 2026-11-24 09:44:19 WARN [Hardware] Disk 2 temperature 51 C Error at line 27. Lines 24-44 of 83 53 % ↑ ↓ PgUp PgDn Home End / next error q quit
viewer.log 83 lines 56 2026-11-25 11:54:26 INFO [Users] admin signed in from 192.0.2.24 (SSH) 57 2026-11-25 13:09:29 INFO [Shares] Folder "Multimedia" scanned, 24 new › 58 2026-11-25 13:12:40 WARN [Users] 5 failed sign-ins for "admin" from 19› 59 2026-11-25 13:12:41 ERROR [Users] 192.0.2.77 blocked for 30 minutes 60 2026-11-25 16:33:29 INFO [Storage] RAID scrubbing of md1 at 72 % 61 2026-11-25 19:44:46 INFO [Hardware] Disk 2 temperature 49 C 62 2026-11-25 20:30:17 INFO [App Center] Hybrid Backup Sync updated 63 2026-11-25 23:30:00 INFO [System] Firmware check: QTS is up to date 64 2026-11-26 00:00:02 INFO [System] Log rotation done, 3 files kept 65 2026-11-26 02:00:01 INFO [Backup] Job "Daily to USB" started 66 2026-11-26 02:35:25 INFO [Backup] Job "Daily to USB" finished, 900 fil› 67 2026-11-26 03:00:00 INFO [Snapshot] Snapshot of DataVol1 taken, 21 kept 68 2026-11-26 07:44:37 INFO [App Center] TurboShell updated 69 2026-11-26 08:48:19 INFO [Hardware] Disk 2 temperature 49 C 70 2026-11-26 11:35:45 INFO [Shares] Folder "Multimedia" scanned, 38 new › 71 2026-11-26 13:53:31 INFO [Users] admin signed in from 192.0.2.24 (SSH) 72 2026-11-26 16:49:44 INFO [Storage] RAID scrubbing of md1 at 47 % 73 2026-11-26 18:21:41 INFO [Users] admin signed in from 192.0.2.10 (SSH) 74 2026-11-26 20:05:11 WARN [Container Station] Container "media" restart› 75 2026-11-26 21:37:54 INFO [Users] admin signed in from 192.0.2.10 (SSH) 76 2026-11-26 23:30:00 INFO [System] Firmware check: QTS is up to date Error at line 59. Lines 56-76 of 83 91 % ↑ ↓ PgUp PgDn Home End / next error q quit
Download it on the NAS
curl -fsSLO https://kb.nasutils.net/projects/pascal/viewer.pasviewer.pas Download viewer.pas
{ Log viewer: scrolls through a log file, warnings in amber, errors in red, and finds the next error.
Build and run: fpc viewer.pas && ./viewer kernel.txt (dmesg > kernel.txt makes one; without a file: viewer.log)
Up/Down and PgUp/PgDn scroll, Home and End jump, / or s finds the next error, q quits. }
program viewer;
{$mode objfpc}{$H+}
uses crt, sysutils, classes;
const
Page = 21; { lines of the log on one screen: lines 2 to 22 }
Room = 72; { characters of a log line that fit beside its number }
var
con: Text;
log: TStringList;
name, note: string;
top, found: integer;
c: char;
function Esc(const code: string): string; begin Result := #27'[' + code + 'm'; end;
function At(x, y: integer): string; begin Result := #27'[' + IntToStr(y) + ';' + IntToStr(x) + 'H'; end;
function Rep(const s: string; n: integer): string; var i: integer; begin Result := ''; for i := 1 to n do Result := Result + s; end;
function Max(a, b: integer): integer; begin if a > b then Result := a else Result := b; end;
function Min(a, b: integer): integer; begin if a < b then Result := a else Result := b; end;
{ The colour of a line by its level: red for an error, amber for a warning }
function Tint(const line: string): string;
begin
if Pos('ERROR', UpperCase(line)) > 0 then Result := '31'
else if Pos('WARN', UpperCase(line)) > 0 then Result := '33'
else Result := '0';
end;
procedure Draw;
var s, line: string; i, n: integer;
begin
s := Esc('0') + At(1, 1) + Esc('30;43') + Format(' %-58s%20s ', [name, IntToStr(log.Count) + ' lines']);
for i := 0 to Page - 1 do
begin
n := top + i;
s := s + Esc('0') + At(1, i + 2) + #27'[K';
if n >= log.Count then Continue;
line := log[n];
if Length(line) > Room then line := Copy(line, 1, Room - 1) + Esc('1;33') + '›';
if n = found then s := s + Esc('30;41') + Format('%5d ', [n + 1]) + Esc('1;37;41') + ' ' + line + Rep(' ', Room - Length(log[n]))
else s := s + Esc('90') + Format('%5d ', [n + 1]) + Esc(Tint(log[n])) + ' ' + line;
end;
s := s + Esc('0') + At(1, 23) + #27'[K' + Esc('33') + note;
s := s + At(1, 24) + Esc('30;47') + Format(' Lines %d-%d of %d %3d %%', [top + 1, Min(top + Page, log.Count), log.Count,
Min(top + Page, log.Count) * 100 div Max(log.Count, 1)]) + ' ↑ ↓ PgUp PgDn Home End / next error q quit ' + #27'[K';
Write(con, s + Esc('0')); Flush(con);
end;
{ The next line after the marked one (or after the top) that holds "error" in any case }
procedure FindError;
var i, from: integer;
begin
if found >= 0 then from := found + 1 else from := top;
for i := from to log.Count - 1 do
if Pos('ERROR', UpperCase(log[i])) > 0 then
begin
found := i; top := Max(0, Min(i - 3, log.Count - Page));
note := Format('Error at line %d.', [i + 1]);
Exit;
end;
note := 'No more errors below. / starts again from the top.';
found := -1; top := 0;
end;
begin
if ParamCount > 0 then name := ParamStr(1) else name := 'viewer.log';
log := TStringList.Create;
try
log.LoadFromFile(name);
except
WriteLn('Cannot read ', name, '.'); Halt(1);
end;
Assign(con, ''); Rewrite(con);
Write(con, #27'[?25l' + #27'[2J');
top := 0; found := -1; note := '';
repeat
Draw;
c := ReadKey; note := '';
if c = #0 then
case ReadKey of
#72: top := top - 1;
#80: top := top + 1;
#73: top := top - Page;
#81: top := top + Page;
#71: top := 0;
#79: top := log.Count - Page;
end
else if c in ['/', 's'] then FindError;
top := Max(0, Min(top, log.Count - Page));
until c in ['q', 'Q', #27];
Write(con, Esc('0') + #27'[2J' + At(1, 1) + #27'[?25h'); Flush(con);
log.Free;
end.
What you learn
TStringList.LoadFromFilereads a whole file into a list of lines;log[n]is line n + 1.- A view is a window over the list:
topis the first line shown, and every key only movestop. Max(0, Min(top, log.Count - Page))keeps the window inside the file in one line.Pos('ERROR', UpperCase(line))finds "error" in any mix of capitals and small letters.try ... exceptcatches a file that cannot be read and ends with a message instead of a crash.
How it works
The program loads the file named after it, or viewer.log, a sample week of a NAS's log. crt reads the keys; the screen goes out as escape codes through con, because crt's own Write counts bytes as columns and the status line holds the arrows ↑ and ↓, three bytes each.
Draw paints the title bar with the file name and its line count, 21 lines of the log, a line for messages and the status line. Each log line gets its number in grey and its colour by level: red when it holds "error", amber when it holds "warn". A line longer than 72 characters ends in an amber ›.
The keys move top: one line with ↑ and ↓, one screen with PgUp and PgDn, to the start with Home and to the end with End. / or s calls FindError. It searches from the line after the last error found, marks the new one in white on red, scrolls so that three lines stand above it and says its number. After the last error it says so and goes back to the top.
Notes to the lines
- 9–1021 lines of the log on lines 2 to 22 of the screen, 72 characters of each beside its number.
- 21–22
MaxandMinfor whole numbers, so the program needs no extra unit for them. - 25–30The colour of a line: the test for "error" comes first, so a line with both words is red.
- 35
%-58s%20sputs the file name on the left and the line count on the right of the title bar. - 39
#27'[K'clears the rest of the screen line, so a short line leaves nothing of the longer one before it. - 42A long line is cut to 71 characters and gets the
›mark. - 43The marked error: number and text in white on red across the whole width.
- 47–48The status line: the lines shown, the share of the file in percent and the keys.
- 56The search goes on after the last error found, or starts at the top line of the screen.
- 60Three lines of context above the error, unless the file ends sooner.
- 71–75A missing or unreadable file ends the program with a message and code 1.
- 82–90crt gives #0 and then a code for the arrow and page keys: 72, 80, 73, 81, 71 and 79.
- 92After every key the window goes back inside the file.
Run it
fpc viewer.pas && ./viewer↑ and ↓ scroll a line, PgUp and PgDn a screen, Home and End jump to the start and the end, / or s finds the next error, and q or Esc quits. dmesg > kernel.txt && ./viewer kernel.txt shows the kernel's messages of the NAS.
Try it yourself
Make / search for "warn" instead: change the text in FindError and its message. Set Room = 60 on line 10 and see more lines end in ›.
More projects in Pascal
- A menu bar with drop-down boxes in Free PascalA Free Pascal program draws a desktop with a menu bar, drop-down boxes and dialog boxes with shadows, and fills the dialogs with the NAS's memory, load, processor, volumes and RAID groups. You learn to draw boxes with escape codes, read keys with crt and run commands with POpen.
- A greeting screen with large letters in Free PascalA Free Pascal program prints the word NAS in large block letters, greets you by the hour and lists the model, kernel, uptime, load and memory in a framed table. You learn records and dynamic arrays, reading the date with dateutils and drawing a table with box characters.
- A NAS summary in two columns in Free PascalA Free Pascal program reads the CPU, temperatures, load, memory, data volumes and network ports into records and prints them as framed panels in two columns. You learn records, dynamic arrays of records, var parameters and a layout built from lists of lines.
- Cable run: a moving game in Free PascalA Free Pascal game where you steer a network cable round a frame with the arrow keys; it grows with every data packet it picks up, and the wall or its own body ends the run. You learn a game loop with KeyPressed and Delay, records for coordinates and drawing only the cells that change.
- A temperature chart of the NAS in Free PascalA Free Pascal program asks QNAP's getsysinfo for the system, CPU and disk temperatures and draws them as a vertical bar chart with a scale, coloured green, amber or red by level. You learn to build a chart line by line from the top and to treat an empty disk bay as a value of its own.
- A log viewer with colours and search in Free PascalA Free Pascal program shows a log file a screen at a time, colours warnings amber and errors red, scrolls with the arrow and page keys and jumps to the next error with /. You learn to load a file into a TStringList, keep a window of lines on the screen and search with UpperCase and Pos.