mirror of
				https://github.com/taigrr/log-socket
				synced 2025-01-18 04:53:14 -08:00 
			
		
		
		
	Adds Flush() to panic and Fatal calls
This commit is contained in:
		
							parent
							
								
									617b1466ce
								
							
						
					
					
						commit
						537840de8b
					
				
							
								
								
									
										11
									
								
								logger.go
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								logger.go
									
									
									
									
									
								
							| @ -55,7 +55,7 @@ func (c *Client) logStdErr() { | |||||||
| func CreateClient() *Client { | func CreateClient() *Client { | ||||||
| 	var client Client | 	var client Client | ||||||
| 	client.initialized = true | 	client.initialized = true | ||||||
| 	client.writer = make(LogWriter, 100) | 	client.writer = make(LogWriter, 1000) | ||||||
| 	sliceTex.Lock() | 	sliceTex.Lock() | ||||||
| 	clients = append(clients, &client) | 	clients = append(clients, &client) | ||||||
| 	sliceTex.Unlock() | 	sliceTex.Unlock() | ||||||
| @ -66,6 +66,7 @@ func Flush() { | |||||||
| 	cleanup.Do(func() { | 	cleanup.Do(func() { | ||||||
| 		close(stderrClient.writer) | 		close(stderrClient.writer) | ||||||
| 		<-stderrFinished | 		<-stderrFinished | ||||||
|  | 		stderrClient.Destroy() | ||||||
| 	}) | 	}) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -100,7 +101,13 @@ func createLog(e Entry) { | |||||||
| 		func(c *Client, e Entry) { | 		func(c *Client, e Entry) { | ||||||
| 			select { | 			select { | ||||||
| 			case c.writer <- e: | 			case c.writer <- e: | ||||||
|  | 				// try to clear out one of the older entries | ||||||
| 			default: | 			default: | ||||||
|  | 				select { | ||||||
|  | 				case <-c.writer: | ||||||
|  | 					c.writer <- e | ||||||
|  | 				default: | ||||||
|  | 				} | ||||||
| 			} | 			} | ||||||
| 		}(c, e) | 		}(c, e) | ||||||
| 	} | 	} | ||||||
| @ -210,6 +217,7 @@ func Panic(args ...interface{}) { | |||||||
| 			// falls through to default below | 			// falls through to default below | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  | 	Flush() | ||||||
| 	panic(errors.New(output)) | 	panic(errors.New(output)) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -224,6 +232,7 @@ func Fatal(args ...interface{}) { | |||||||
| 		level:     LFatal, | 		level:     LFatal, | ||||||
| 	} | 	} | ||||||
| 	createLog(e) | 	createLog(e) | ||||||
|  | 	Flush() | ||||||
| 	os.Exit(1) | 	os.Exit(1) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,17 +1,23 @@ | |||||||
| package lambo_log_socket | package lambo_log_socket | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"strconv" | ||||||
|  | 	"sync" | ||||||
| 	"testing" | 	"testing" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | // Test CreateClient() and Client.Destroy() | ||||||
| func TestCreateDestroy(t *testing.T) { | func TestCreateDestroy(t *testing.T) { | ||||||
|  | 	// Ensure only stderr exists at the beginning | ||||||
| 	if len(clients) != 1 { | 	if len(clients) != 1 { | ||||||
| 		t.Errorf("Expected 1 client, but found %d", len(clients)) | 		t.Errorf("Expected 1 client, but found %d", len(clients)) | ||||||
| 	} | 	} | ||||||
|  | 	// Create a new client, ensure it's added | ||||||
| 	c := CreateClient() | 	c := CreateClient() | ||||||
| 	if len(clients) != 2 { | 	if len(clients) != 2 { | ||||||
| 		t.Errorf("Expected 2 clients, but found %d", len(clients)) | 		t.Errorf("Expected 2 clients, but found %d", len(clients)) | ||||||
| 	} | 	} | ||||||
|  | 	// Destroy it and ensure it's actually removed from the array | ||||||
| 	c.Destroy() | 	c.Destroy() | ||||||
| 	if len(clients) != 1 { | 	if len(clients) != 1 { | ||||||
| 		t.Errorf("Expected 1 client, but found %d", len(clients)) | 		t.Errorf("Expected 1 client, but found %d", len(clients)) | ||||||
| @ -31,18 +37,31 @@ func TestSetLogLevel(t *testing.T) { | |||||||
| 	c.Destroy() | 	c.Destroy() | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Trace prints out logs on trace level | func BenchmarkDebugSerial(b *testing.B) { | ||||||
| func TestTrace(t *testing.T) { | 	c := CreateClient() | ||||||
| 	testString := "Testing trace!" | 	var x sync.WaitGroup | ||||||
|  | 	x.Add(b.N) | ||||||
|  | 	for i := 0; i < b.N; i++ { | ||||||
|  | 		Debug(i) | ||||||
|  | 		go func() { | ||||||
|  | 			c.Get() | ||||||
|  | 			x.Done() | ||||||
|  | 		}() | ||||||
|  | 	} | ||||||
|  | 	x.Wait() | ||||||
|  | 	c.Destroy() | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Trace ensure logs come out in the right order | ||||||
|  | func TestOrder(t *testing.T) { | ||||||
|  | 	testString := "Testing trace: " | ||||||
| 	var c *Client | 	var c *Client | ||||||
| 	c = CreateClient() | 	c = CreateClient() | ||||||
| 	c.SetLogLevel(LTrace) | 	c.SetLogLevel(LTrace) | ||||||
| 
 | 
 | ||||||
| 	for i := 0; i < 5; i++ { | 	for i := 0; i < 5000; i++ { | ||||||
| 		Trace(testString) | 		Trace(testString + strconv.Itoa(i)) | ||||||
| 	} | 		if testString+strconv.Itoa(i) != c.Get().Output { | ||||||
| 	for i := 0; i < 5; i++ { |  | ||||||
| 		if testString != c.Get().Output { |  | ||||||
| 			t.Error("Trace input doesn't match output") | 			t.Error("Trace input doesn't match output") | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user