@@ -6,7 +6,6 @@ package work
66import (
77 "errors"
88 "fmt"
9- "log"
109 "sync"
1110 "sync/atomic"
1211 "time"
@@ -43,10 +42,12 @@ type Work struct {
4342 routines int64 // Number of routines
4443 active int64 // Active number of routines in the work pool.
4544 pending int64 // Pending number of routines waiting to submit work.
45+
46+ logFunc func (message string ) // Function called to providing logging support
4647}
4748
4849// New creates a new Worker.
49- func New (minRoutines int , statTime time.Duration ) (* Work , error ) {
50+ func New (minRoutines int , statTime time.Duration , logFunc func ( message string ) ) (* Work , error ) {
5051 if minRoutines <= 0 {
5152 return nil , ErrorInvalidMinRoutines
5253 }
@@ -62,6 +63,7 @@ func New(minRoutines int, statTime time.Duration) (*Work, error) {
6263 control : make (chan int ),
6364 kill : make (chan struct {}),
6465 shutdown : make (chan struct {}),
66+ logFunc : logFunc ,
6567 }
6668
6769 // Start the manager.
@@ -113,7 +115,7 @@ done:
113115 atomic .AddInt64 (& w .routines , - 1 )
114116 w .wg .Done ()
115117
116- log . Println ( "Work : gr : Info : Shutdown " )
118+ w . log ( "Worker : Shutting Down " )
117119}
118120
119121// Run wait for the goroutine pool to take the work
@@ -138,7 +140,7 @@ func (w *Work) manager() {
138140 w .wg .Add (1 )
139141
140142 go func () {
141- log . Println ("Work : manager : Started" )
143+ w . log ("Work Manager : Started" )
142144
143145 // Create a timer to run stats.
144146 timer := time .NewTimer (w .statTime )
@@ -161,7 +163,7 @@ func (w *Work) manager() {
161163 case c := <- w .control :
162164 switch c {
163165 case addRoutine :
164- log . Println ("Work : manager : Info : Add Routine" )
166+ w . log ("Work Manager : Add Routine" )
165167
166168 // Capture a unique id.
167169 w .counter ++
@@ -174,14 +176,14 @@ func (w *Work) manager() {
174176 go w .work (w .counter )
175177
176178 case rmvRoutine :
177- log . Println ("Work : manager : Info : Remove Routine" )
179+ w . log ("Work Manager : Remove Routine" )
178180
179181 // Capture the number of routines.
180182 routines := int (atomic .LoadInt64 (& w .routines ))
181183
182184 // Are there routines to remove.
183185 if routines <= w .minRoutines {
184- log . Println ("Work : manager : Info : Remove Routine Cancelled " )
186+ w . log ("Work Manager : Reached Minimum Can't Remove " )
185187 break
186188 }
187189
@@ -196,11 +198,18 @@ func (w *Work) manager() {
196198 active := atomic .LoadInt64 (& w .active )
197199
198200 // Display the stats.
199- fmt .Printf ("Work : manager : Stats : G[%d] P[%d] A[%d]\n " , routines , pending , active )
201+ w . log ( fmt .Sprintf ("Work Manager : Stats : G[%d] P[%d] A[%d]" , routines , pending , active ) )
200202
201203 // Reset the clock.
202204 timer .Reset (w .statTime )
203205 }
204206 }
205207 }()
206208}
209+
210+ // log sending logging messages back to the client.
211+ func (w * Work ) log (message string ) {
212+ if w .logFunc != nil {
213+ w .logFunc (message )
214+ }
215+ }
0 commit comments