mirror of
				https://github.com/superseriousbusiness/gotosocial.git
				synced 2025-10-31 14:52:30 -05:00 
			
		
		
		
	[chore] simplify logging to remove "safetywriter" given os.File has concurrency protection (#1083)
Signed-off-by: kim <grufwub@gmail.com> Signed-off-by: kim <grufwub@gmail.com>
This commit is contained in:
		
					parent
					
						
							
								2eea3d57e0
							
						
					
				
			
			
				commit
				
					
						4a9538593c
					
				
			
		
					 2 changed files with 4 additions and 46 deletions
				
			
		|  | @ -20,7 +20,6 @@ package log | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" |  | ||||||
| 	"log/syslog" | 	"log/syslog" | ||||||
| 	"os" | 	"os" | ||||||
| 	"strings" | 	"strings" | ||||||
|  | @ -39,10 +38,6 @@ var ( | ||||||
| 	// lvlstrs is the lookup table of log levels to strings. | 	// lvlstrs is the lookup table of log levels to strings. | ||||||
| 	lvlstrs = level.Default() | 	lvlstrs = level.Default() | ||||||
| 
 | 
 | ||||||
| 	// Preprepared stdout/stderr log writers. |  | ||||||
| 	stdout = &safewriter{w: os.Stdout} |  | ||||||
| 	stderr = &safewriter{w: os.Stderr} |  | ||||||
| 
 |  | ||||||
| 	// Syslog output, only set if enabled. | 	// Syslog output, only set if enabled. | ||||||
| 	sysout *syslog.Writer | 	sysout *syslog.Writer | ||||||
| 
 | 
 | ||||||
|  | @ -187,12 +182,12 @@ func printf(depth int, fields []kv.Field, s string, a ...interface{}) { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// Write to log and release | 	// Write to log and release | ||||||
| 	_, _ = stdout.Write(buf.B) | 	_, _ = os.Stdout.Write(buf.B) | ||||||
| 	putBuf(buf) | 	putBuf(buf) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func logf(depth int, lvl level.LEVEL, fields []kv.Field, s string, a ...interface{}) { | func logf(depth int, lvl level.LEVEL, fields []kv.Field, s string, a ...interface{}) { | ||||||
| 	var out io.Writer | 	var out *os.File | ||||||
| 
 | 
 | ||||||
| 	// Check if enabled. | 	// Check if enabled. | ||||||
| 	if lvl > Level() { | 	if lvl > Level() { | ||||||
|  | @ -202,9 +197,9 @@ func logf(depth int, lvl level.LEVEL, fields []kv.Field, s string, a ...interfac | ||||||
| 	// Split errors to stderr, | 	// Split errors to stderr, | ||||||
| 	// all else goes to stdout. | 	// all else goes to stdout. | ||||||
| 	if lvl <= level.ERROR { | 	if lvl <= level.ERROR { | ||||||
| 		out = stderr | 		out = os.Stderr | ||||||
| 	} else { | 	} else { | ||||||
| 		out = stdout | 		out = os.Stdout | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// Acquire buffer | 	// Acquire buffer | ||||||
|  |  | ||||||
|  | @ -1,37 +0,0 @@ | ||||||
| /* |  | ||||||
|    GoToSocial |  | ||||||
|    Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org |  | ||||||
| 
 |  | ||||||
|    This program is free software: you can redistribute it and/or modify |  | ||||||
|    it under the terms of the GNU Affero General Public License as published by |  | ||||||
|    the Free Software Foundation, either version 3 of the License, or |  | ||||||
|    (at your option) any later version. |  | ||||||
| 
 |  | ||||||
|    This program is distributed in the hope that it will be useful, |  | ||||||
|    but WITHOUT ANY WARRANTY; without even the implied warranty of |  | ||||||
|    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the |  | ||||||
|    GNU Affero General Public License for more details. |  | ||||||
| 
 |  | ||||||
|    You should have received a copy of the GNU Affero General Public License |  | ||||||
|    along with this program.  If not, see <http://www.gnu.org/licenses/>. |  | ||||||
| */ |  | ||||||
| 
 |  | ||||||
| package log |  | ||||||
| 
 |  | ||||||
| import ( |  | ||||||
| 	"io" |  | ||||||
| 	"sync" |  | ||||||
| ) |  | ||||||
| 
 |  | ||||||
| // safewriter wraps a writer to provide mutex safety on write. |  | ||||||
| type safewriter struct { |  | ||||||
| 	w io.Writer |  | ||||||
| 	m sync.Mutex |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func (w *safewriter) Write(b []byte) (int, error) { |  | ||||||
| 	w.m.Lock() |  | ||||||
| 	n, err := w.w.Write(b) |  | ||||||
| 	w.m.Unlock() |  | ||||||
| 	return n, err |  | ||||||
| } |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue