|
| 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 | + ``` |
0 commit comments