Skip to content

Commit 168c27c

Browse files
committed
docs: Add code guidelines for logging
1 parent 56d23f7 commit 168c27c

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

Docs/Code Guidelines.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Open Rails Code Guidelines
2+
3+
## Logging
4+
5+
- Use the most appropriate [logging severity](#logging-severity)
6+
- Use the most appropriate logging method:
7+
- When parsing files, use [file format logging methods](#file-format-logging-methods)
8+
- Otherwise, use [general logging methods](#general-logging-methods)
9+
- Use [simple past tense](https://en.wikipedia.org/wiki/Simple_past) with the verb at the front; e.g. "Ignored missing texture file" instead of "Missing texture file was ignored".
10+
- Do not make reference to the user or Open Rails itself
11+
- Include the most relevant source file and line number at the end of the message (_file format logging methods_ do this for you)
12+
13+
### Logging severity
14+
15+
- Error: Fatal problem where continuing is not possible (only used in very limited places)
16+
- Warning: Resolved problem or manageable bad data (e.g. data that is missing/duplicate/unknown/unexpected)
17+
- Ignored missing ...
18+
- Ignored duplicate ...
19+
- Skipped unknown ...
20+
- Expected ...; got ...
21+
- Information: No problem but is notable (e.g used default for important but commonly missing data)
22+
- Used default for ...
23+
24+
### File format logging methods
25+
26+
When parsing files, use the specific logger methods below to automatically include accurate source information (file name, line number):
27+
28+
- When using `JsonReader`, use `JsonReader.TraceWarning` and `JsonReader.TraceInformation`
29+
- When using `SBR.Open`, use `SBR.TraceWarning` and `SBR.TraceInformation`
30+
- When using `STFReader`, use `STFException.TraceWarning` and `STFException.TraceInformation` (static methods)
31+
32+
### General logging methods
33+
34+
- For application start-up (adjacent to existing code), use [Console.Write](https://learn.microsoft.com/en-gb/dotnet/api/system.console.write) and [Console.WriteLine](https://learn.microsoft.com/en-gb/dotnet/api/system.console.writeline)
35+
- For exceptions when loading a file, use [Trace.WriteLine](https://learn.microsoft.com/en-gb/dotnet/api/system.diagnostics.trace.writeline) as follows:
36+
```csharp
37+
try
38+
{
39+
// Something that might break
40+
}
41+
catch (Exception error)
42+
{
43+
Trace.WriteLine(new FileLoadException(fileName, error));
44+
}
45+
```
46+
- For exceptions in other cases, use [Trace.WriteLine](https://learn.microsoft.com/en-gb/dotnet/api/system.diagnostics.trace.writeline) as follows:
47+
```csharp
48+
try
49+
{
50+
// Something that might break
51+
}
52+
catch (Exception error)
53+
{
54+
Trace.WriteLine(error);
55+
}
56+
```
57+
- Otherwise, use [Trace.TraceError](https://learn.microsoft.com/en-gb/dotnet/api/system.diagnostics.trace.traceerror), [Trace.TraceWarning](https://learn.microsoft.com/en-gb/dotnet/api/system.diagnostics.trace.tracewarning), [Trace.TraceInformation](https://learn.microsoft.com/en-gb/dotnet/api/system.diagnostics.trace.traceinformation), for example:
58+
```csharp
59+
Trace.TraceError("Player locomotive {1} cannot be loaded in {0}", conFileName, wagonFileName);
60+
Trace.TraceWarning("Skipped unknown texture addressing mode {1} in shape {0}", shapeFileName, addressingMode);
61+
Trace.TraceInformation("Ignored missing animation data in shape {0}", shapeFileName);
62+
```

Docs/Contributing.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ Code is expected to follow the [Microsoft .NET Framework Design Guidelines](http
117117

118118
Code style (placement of braces, etc.) is expected to follow the default Visual Studio rules; the [Microsoft .NET C# Coding Conventions](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions) provides a good basis for many aspects of this.
119119

120+
Code should be consistent with the [Open Rails Code Guidelines](Code%20Guidelines.md).
121+
120122
### Architecture requirements
121123

122124
Code should be well structured, with small methods performing a single key task (indicated by their name), and larger complex operations formed through calls to multiple smaller methods.

0 commit comments

Comments
 (0)