Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
L
LcdLogger
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Antoni Zaprzała
LcdLogger
Commits
fab48c2e
Commit
fab48c2e
authored
Jul 01, 2026
by
Antoni Zaprzała
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update 2
parent
92edfcc2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
87 deletions
+17
-87
Program.cs
LcdLogger/Program.cs
+6
-5
LogFileBrowser.cs
LcdLogger/Services/LogFileBrowser.cs
+11
-50
LogFileManager.cs
LcdLogger/Services/LogFileManager.cs
+0
-32
No files found.
LcdLogger/Program.cs
View file @
fab48c2e
...
@@ -30,20 +30,21 @@ app.MapGet("/files", (LogFileBrowser browser) =>
...
@@ -30,20 +30,21 @@ app.MapGet("/files", (LogFileBrowser browser) =>
// downloads to working directory a single log file by name
// downloads to working directory a single log file by name
// example: curl -OJ http://127.0.0.1:8082/files/download/lcd-2026-07-01_09-18-33.log
// example: curl -OJ http://127.0.0.1:8082/files/download/lcd-2026-07-01_09-18-33.log
app
.
MapGet
(
"/files/download/{name}"
,
async
(
string
name
,
LogFileBrowser
browser
,
CancellationToken
ct
)
=>
app
.
MapGet
(
"/files/download/{name}"
,
(
string
name
,
LogFileBrowser
browser
)
=>
{
{
var
bytes
=
await
browser
.
ReadSnapshotAsync
(
name
,
ct
);
var
bytes
=
browser
.
ReadSnapshot
(
name
);
if
(
bytes
is
null
)
return
Results
.
NotFound
();
if
(
bytes
is
null
)
return
Results
.
NotFound
();
return
Results
.
File
(
bytes
,
"application/octet-stream"
,
fileDownloadName
:
name
);
return
Results
.
File
(
bytes
,
"application/octet-stream"
,
fileDownloadName
:
name
);
});
});
// example: curl -OJ http://127.0.0.1:8082/files/download-all
// example: curl -OJ http://127.0.0.1:8082/files/download-all
app
.
MapGet
(
"/files/download-all"
,
async
(
LogFileBrowser
browser
,
CancellationToken
ct
)
=>
app
.
MapGet
(
"/files/download-all"
,
(
LogFileBrowser
browser
)
=>
{
{
var
zipBytes
=
await
browser
.
BuildZipAsync
(
ct
);
var
zipBytes
=
browser
.
BuildZip
(
);
var
zipName
=
$"lcd-logs-
{
DateTime
.
Now
:
yyyy
-
MM
-
dd_HH
-
mm
-
ss
}
.zip"
;
if
(
zipBytes
is
null
)
return
Results
.
NotFound
()
;
var
zipName
=
$"lcd-logs-
{
DateTime
.
Now
:
yyyy
-
MM
-
dd_HH
-
mm
-
ss
}
.zip"
;
return
Results
.
File
(
zipBytes
,
"application/zip"
,
fileDownloadName
:
zipName
);
return
Results
.
File
(
zipBytes
,
"application/zip"
,
fileDownloadName
:
zipName
);
});
});
...
...
LcdLogger/Services/LogFileBrowser.cs
View file @
fab48c2e
...
@@ -44,7 +44,7 @@ namespace LcdLogger.Services
...
@@ -44,7 +44,7 @@ namespace LcdLogger.Services
return
File
.
Exists
(
fullPath
)
?
fullPath
:
null
;
return
File
.
Exists
(
fullPath
)
?
fullPath
:
null
;
}
}
public
async
Task
<
byte
[]?>
ReadSnapshotAsync
(
string
name
,
CancellationToken
cancellationToken
=
default
)
public
byte
[]?
ReadSnapshot
(
string
name
)
{
{
var
path
=
GetFilePath
(
name
);
var
path
=
GetFilePath
(
name
);
if
(
path
is
null
)
return
null
;
if
(
path
is
null
)
return
null
;
...
@@ -55,26 +55,26 @@ namespace LcdLogger.Services
...
@@ -55,26 +55,26 @@ namespace LcdLogger.Services
path
,
path
,
FileMode
.
Open
,
FileMode
.
Open
,
FileAccess
.
Read
,
FileAccess
.
Read
,
FileShare
.
ReadWrite
,
FileShare
.
ReadWrite
);
bufferSize
:
4096
,
useAsync
:
true
);
return
await
ReadExactSnapshotAsync
(
fileStream
,
name
,
fileStream
.
Length
,
cancellationToken
);
var
buffer
=
new
byte
[
fileStream
.
Length
];
fileStream
.
ReadExactly
(
buffer
);
return
buffer
;
}
}
catch
(
IOException
e
)
catch
(
Exception
e
)
when
(
e
is
IOException
or
EndOfStreamException
)
{
{
_logger
.
LogError
(
e
,
"Failed to read snapshot of {FileName}"
,
name
);
_logger
.
LogError
(
e
,
"Failed to read snapshot of {FileName}"
,
name
);
return
null
;
return
null
;
}
}
}
}
public
async
Task
<
byte
[
]>
BuildZipAsync
(
CancellationToken
cancellationToken
=
default
)
public
byte
[]?
BuildZip
(
)
{
{
var
directoryInfo
=
new
DirectoryInfo
(
_options
.
Directory
);
var
directoryInfo
=
new
DirectoryInfo
(
_options
.
Directory
);
if
(!
directoryInfo
.
Exists
)
if
(!
directoryInfo
.
Exists
)
{
{
_logger
.
LogWarning
(
"Directory {Directory} does not exist"
,
_options
.
Directory
);
_logger
.
LogWarning
(
"Directory {Directory} does not exist"
,
_options
.
Directory
);
return
Array
.
Empty
<
byte
>()
;
return
null
;
}
}
using
var
buffer
=
new
MemoryStream
();
using
var
buffer
=
new
MemoryStream
();
...
@@ -85,7 +85,6 @@ namespace LcdLogger.Services
...
@@ -85,7 +85,6 @@ namespace LcdLogger.Services
{
{
try
try
{
{
var
snapshotLength
=
file
.
Length
;
var
entry
=
zip
.
CreateEntry
(
file
.
Name
,
CompressionLevel
.
Fastest
);
var
entry
=
zip
.
CreateEntry
(
file
.
Name
,
CompressionLevel
.
Fastest
);
using
(
var
entryStream
=
entry
.
Open
())
using
(
var
entryStream
=
entry
.
Open
())
...
@@ -95,7 +94,9 @@ namespace LcdLogger.Services
...
@@ -95,7 +94,9 @@ namespace LcdLogger.Services
FileAccess
.
Read
,
FileAccess
.
Read
,
FileShare
.
ReadWrite
))
FileShare
.
ReadWrite
))
{
{
await
CopyExactAsync
(
fileStream
,
entryStream
,
snapshotLength
,
cancellationToken
);
var
content
=
new
byte
[
file
.
Length
];
fileStream
.
ReadExactly
(
content
);
entryStream
.
Write
(
content
);
}
}
}
}
catch
(
Exception
e
)
catch
(
Exception
e
)
...
@@ -108,45 +109,5 @@ namespace LcdLogger.Services
...
@@ -108,45 +109,5 @@ namespace LcdLogger.Services
return
buffer
.
ToArray
();
return
buffer
.
ToArray
();
}
}
private
async
Task
<
byte
[
]>
ReadExactSnapshotAsync
(
Stream
source
,
string
name
,
long
length
,
CancellationToken
cancellationToken
)
{
var
buffer
=
new
byte
[
length
];
var
offset
=
0
;
while
(
offset
<
buffer
.
Length
)
{
var
read
=
await
source
.
ReadAsync
(
buffer
.
AsMemory
(
offset
),
cancellationToken
);
if
(
read
==
0
)
break
;
offset
+=
read
;
}
if
(
offset
<
buffer
.
Length
)
{
_logger
.
LogWarning
(
"Snapshot {FileName} truncated: expected {Expected} bytes, read {Actual}"
,
name
,
buffer
.
Length
,
offset
);
return
buffer
[..
offset
];
}
return
buffer
;
}
private
static
async
Task
CopyExactAsync
(
Stream
source
,
Stream
destination
,
long
count
,
CancellationToken
cancellationToken
)
{
var
buffer
=
new
byte
[
81920
];
var
remaining
=
count
;
while
(
remaining
>
0
)
{
var
toRead
=
(
int
)
Math
.
Min
(
buffer
.
Length
,
remaining
);
var
read
=
await
source
.
ReadAsync
(
buffer
.
AsMemory
(
0
,
toRead
),
cancellationToken
);
if
(
read
==
0
)
break
;
await
destination
.
WriteAsync
(
buffer
.
AsMemory
(
0
,
read
),
cancellationToken
);
remaining
-=
read
;
}
}
}
}
}
}
LcdLogger/Services/LogFileManager.cs
View file @
fab48c2e
...
@@ -106,45 +106,13 @@ namespace LcdLogger.Services
...
@@ -106,45 +106,13 @@ namespace LcdLogger.Services
await
_fileStream
.
FlushAsync
(
cancellationToken
);
await
_fileStream
.
FlushAsync
(
cancellationToken
);
await
_fileStream
.
DisposeAsync
();
await
_fileStream
.
DisposeAsync
();
//var justClosedFile = _currentPath;
//_ = Task.Run(async () =>
//{
// await CompressAsync(justClosedFile);
//});
await
PruneSurplusFilesAsync
(
cancellationToken
);
await
PruneSurplusFilesAsync
(
cancellationToken
);
await
OpenNewAsync
(
cancellationToken
);
await
OpenNewAsync
(
cancellationToken
);
}
}
//private async Task CompressAsync(string path)
//{
// try
// {
// var gzPath = path + ".gz";
// using (var source = File.OpenRead(path))
// using (var destination = File.Create(gzPath))
// using (var gzip = new GZipStream(destination, CompressionLevel.SmallestSize))
// {
// await source.CopyToAsync(gzip);
// }
// File.Delete(path);
// }
// catch (Exception e)
// {
// _logger.LogError(e.Message, "Compress failed");
// }
//}
private
bool
_disposed
;
public
async
ValueTask
DisposeAsync
()
public
async
ValueTask
DisposeAsync
()
{
{
if
(
_disposed
)
return
;
_disposed
=
true
;
try
try
{
{
await
_fileStream
.
FlushAsync
();
await
_fileStream
.
FlushAsync
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment