-
Notifications
You must be signed in to change notification settings - Fork 0
TID #007 Logging: Create a logger for both the app and the machine #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
loerac
wants to merge
14
commits into
trunk
Choose a base branch
from
TID007-logging-dev
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f15f303
Log if whether it's time to feed the cat or not
a86b3ab
Updated logger to roll over on existing log files
loerac ee79920
Use Logger for logging
loerac a54db14
Log if whether it's time to feed the cat or not
ea70ded
Updated logger to roll over on existing log files
loerac 8b41301
Use Logger for logging
loerac 4560544
Merge branch 'logging-dev' of https://github.com/loerac/cat-feeder in…
loerac 5c2a96f
Added logs for cat-app
loerac 31a12bd
Don't use pointer
loerac 6dfcc82
Added comments and changed logs/labels
c3a14c7
Changed logs/labels
bc4e9fd
Changed the logger to be golog
e953b2e
Remove unneeded imports
177117e
Updated per comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import logging | ||
|
|
||
| ### | ||
| # @brief: Create new logging object with the identity of `name` | ||
| # | ||
| # @arg: name - The name to give the identity of the log | ||
| # | ||
| # @return: Logger object | ||
| ### | ||
| def Applogger(name): | ||
| logging.basicConfig(level=logging.INFO, | ||
| filename='/tmp/cat-feeder.log', | ||
| format='%(asctime)s %(name)s - %(message)s', | ||
| datefmt='%Y/%m/%d %H:%M:%S' | ||
| ) | ||
|
|
||
| logger = logging.getLogger(name) | ||
|
|
||
| return logger | ||
|
|
||
| ### | ||
| # @brief: Turn the feeding time input to the time format | ||
| # | ||
| # @arg: feeding_time - Array holding the hour and minute | ||
| # | ||
| # @return: String formated time | ||
| ### | ||
| def PrettyTime(feeding_times): | ||
| hour = feeding_times[0] | ||
| minute = feeding_times[1] | ||
|
|
||
| return str(hour) + ":" + (str(minute) if int(minute) > 9 else "0" + str(minute)) | ||
|
|
||
| ### | ||
| # @brief: Parse the OS error to return the error message | ||
| # | ||
| # @arg: err - OSError from try-except | ||
| # | ||
| # @return: String formatted error message | ||
| ### | ||
| def StrOSError(err): | ||
| return str(err.args[0].reason)[str(err.args[0].reason).find(":") + 2:] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| type Golog struct { | ||
| Lock sync.Mutex | ||
| Fpath *os.File | ||
| Prefix string | ||
| } | ||
|
|
||
| const LogFilename string = "/tmp/cat-feeder.log" | ||
|
|
||
| /** | ||
| * @brief: Checks to see if the log file is present from a previous | ||
| * execution. If true, roll over log with timestamp. | ||
| * | ||
| * @return: nil on success, else error | ||
| **/ | ||
| func InitGolog() error { | ||
|
loerac marked this conversation as resolved.
|
||
| finfo, err := os.Stat(LogFilename) | ||
| if err == nil { | ||
| path := strings.Split(LogFilename, finfo.Name())[0] | ||
| err = os.Rename( | ||
| LogFilename, | ||
| path + time.Now().Format("20060102T150405") + "-" + finfo.Name(), | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| /** | ||
| * @brief: Opens and creates a log file. If no errors opening/creating, | ||
| * then create a Golog with prefix and file pointer | ||
| * | ||
| * @arg: prefix - String to prepend to the log message | ||
| * | ||
| * @return: New logger, nil if error | ||
| **/ | ||
| func OpenGolog(prefix string) *Golog { | ||
| fpath, err := os.OpenFile(LogFilename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666) | ||
|
|
||
| if err != nil { | ||
| return nil | ||
| } | ||
|
|
||
| return &Golog{ | ||
| Prefix: prefix, | ||
| Fpath: fpath, | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @brief: Write log message to the log file. Log message contains: | ||
| * - Timestamp | ||
| * - Prefix | ||
| * - Message | ||
| * | ||
| * @arg: msg - Message to write to log file | ||
| * | ||
| * @return: int - How many bytes were written | ||
| * error - Errors while writing | ||
| **/ | ||
| func (gl *Golog) Println(msg string) (int, error) { | ||
| gl.Lock.Lock() | ||
| defer gl.Lock.Unlock() | ||
|
|
||
| output := []byte(time.Now().Format("2006/01/02 15:04:05") + " " + gl.Prefix + " - " + msg + "\n") | ||
| return gl.Fpath.Write(output) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.