github.com/pavlo67/common@v0.5.3/common/logger/logger_test/test.go (about)

     1  package logger_test
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/pavlo67/common/common"
    11  
    12  	"github.com/pavlo67/common/common/pnglib"
    13  
    14  	"github.com/pavlo67/common/common/filelib"
    15  
    16  	"github.com/pavlo67/common/common/logger"
    17  )
    18  
    19  func New(t *testing.T, key, basePath string, saveFiles bool, commentPaths []string) logger.Operator {
    20  	return &loggerTest{t: t, key: key, basePath: basePath, saveFiles: saveFiles, commentPaths: commentPaths}
    21  }
    22  
    23  //func InitComments(t *testing.T) logger.OperatorComments {
    24  //	return &loggerTest{t}
    25  //}
    26  
    27  var _ logger.Operator = &loggerTest{}
    28  
    29  type loggerTest struct {
    30  	t             *testing.T
    31  	key, basePath string
    32  	saveFiles     bool
    33  	commentPaths  []string
    34  }
    35  
    36  //func (sl *loggerTest) Comment(text string) {
    37  //	sl.Info(text)
    38  //}
    39  
    40  func (op *loggerTest) Debug(args ...interface{}) {
    41  	if op != nil && op.t != nil {
    42  		op.t.Log(append([]interface{}{"DEBUG: "}, args...)...)
    43  	} else {
    44  		log.Print(append([]interface{}{"DEBUG: "}, args...)...)
    45  	}
    46  }
    47  
    48  func (op *loggerTest) Debugf(template string, args ...interface{}) {
    49  	if op != nil && op.t != nil {
    50  		op.t.Logf("DEBUG: "+template, args...)
    51  	} else {
    52  		log.Printf("DEBUG: "+template, args...)
    53  	}
    54  }
    55  
    56  func (op *loggerTest) Info(args ...interface{}) {
    57  	if op != nil && op.t != nil {
    58  		op.t.Log(append([]interface{}{"INFO: "}, args...)...)
    59  	} else {
    60  		log.Print(append([]interface{}{"INFO: "}, args...)...)
    61  	}
    62  }
    63  
    64  func (op *loggerTest) Infof(template string, args ...interface{}) {
    65  	if op != nil && op.t != nil {
    66  		op.t.Logf("INFO: "+template, args...)
    67  	} else {
    68  		log.Printf("INFO: "+template, args...)
    69  	}
    70  }
    71  
    72  func (op *loggerTest) Warn(args ...interface{}) {
    73  	if op != nil && op.t != nil {
    74  		op.t.Log(append([]interface{}{"WARN: "}, args...)...)
    75  	} else {
    76  		log.Print(append([]interface{}{"WARN: "}, args...)...)
    77  	}
    78  }
    79  
    80  func (op *loggerTest) Warnf(template string, args ...interface{}) {
    81  	if op != nil && op.t != nil {
    82  		op.t.Logf("WARN: "+template, args...)
    83  	} else {
    84  		log.Printf("WARN: "+template, args...)
    85  	}
    86  }
    87  
    88  func (op *loggerTest) Error(args ...interface{}) {
    89  	if op != nil && op.t != nil {
    90  		op.t.Error(args...)
    91  	} else {
    92  		log.Print(append([]interface{}{"ERROR: "}, args...)...)
    93  	}
    94  }
    95  
    96  func (op *loggerTest) Errorf(template string, args ...interface{}) {
    97  	if op != nil && op.t != nil {
    98  		op.t.Errorf(template, args...)
    99  	} else {
   100  		log.Printf("ERROR: "+template, args...)
   101  	}
   102  }
   103  
   104  func (op *loggerTest) Fatal(args ...interface{}) {
   105  	if op != nil && op.t != nil {
   106  		op.t.Fatal(args...)
   107  	} else {
   108  		log.Fatal(args...)
   109  	}
   110  }
   111  
   112  func (op *loggerTest) Fatalf(template string, args ...interface{}) {
   113  	if op != nil && op.t != nil {
   114  		op.t.Fatalf(template, args...)
   115  	} else {
   116  		log.Fatalf(template, args...)
   117  	}
   118  
   119  }
   120  func (op loggerTest) Comment(text string) {
   121  	outstring := "\n\t\t" + text + "\n\n"
   122  	for _, outPath := range op.commentPaths {
   123  		switch outPath {
   124  		case "stdout":
   125  			fmt.Print(outstring)
   126  		case "stderr":
   127  			// to prevent duplicates in console
   128  			// fmt.Fprint(os.Stderr, outPath+" "+outstring)
   129  		default:
   130  			f, err := os.OpenFile(outPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
   131  			if err != nil {
   132  				fmt.Fprint(os.Stderr, err)
   133  			}
   134  			defer f.Close()
   135  			if _, err := f.WriteString(outstring); err != nil {
   136  				fmt.Fprint(os.Stderr, err)
   137  			}
   138  		}
   139  	}
   140  
   141  }
   142  
   143  func (op *loggerTest) SetPath(basePath string) {
   144  	if op == nil {
   145  		return
   146  	}
   147  
   148  	if basePath = strings.TrimSpace(basePath); basePath == "" {
   149  		op.basePath = ""
   150  		return
   151  	}
   152  
   153  	var err error
   154  	if basePath, err = filelib.Dir(basePath); err != nil {
   155  		op.Errorf("can't create basePath (%s): %s / on logger.SetPath()", basePath, err)
   156  		return
   157  	}
   158  
   159  	op.basePath = basePath
   160  }
   161  
   162  func (op loggerTest) File(path string, data []byte) {
   163  	if op.saveFiles {
   164  		basedPaths, err := logger.ModifyPaths([]string{path}, op.basePath)
   165  		if err != nil {
   166  			op.Error(err)
   167  		} else if err := os.WriteFile(basedPaths[0], data, 0644); err != nil {
   168  			op.Errorf("CAN'T WRITE TO FILE %s: %s", path, err)
   169  		}
   170  	}
   171  }
   172  
   173  //func (op loggerTest) Image(path string, getImage logger.GetImage) {
   174  //	if op.saveFiles {
   175  //		img, info, err := getImage.Image(nil)
   176  //		if info != "" {
   177  //			op.Info(info)
   178  //		}
   179  //		if img != nil {
   180  //			basedPaths, err := logger.ModifyPaths([]string{path}, op.basePath)
   181  //			if err != nil {
   182  //				op.Error(err)
   183  //			} else if err = pnglib.Save(img, basedPaths[0]); err != nil {
   184  //				op.Error(err)
   185  //			}
   186  //		}
   187  //		if err != nil {
   188  //			op.Error(err)
   189  //		}
   190  //	}
   191  //}
   192  
   193  func (op loggerTest) Image(path string, getImage logger.GetImage, opts common.Map) {
   194  	if op.saveFiles {
   195  		img, info, err := getImage.Image(opts)
   196  		if info != "" {
   197  			op.Info(info)
   198  		}
   199  		if img != nil {
   200  			basedPaths, err := logger.ModifyPaths([]string{path}, op.basePath)
   201  			if err != nil {
   202  				op.Error(err)
   203  			} else if err = pnglib.Save(img, basedPaths[0]); err != nil {
   204  				op.Error(err)
   205  			}
   206  		}
   207  		if err != nil {
   208  			op.Error(err)
   209  		}
   210  	}
   211  }
   212  
   213  func (op *loggerTest) SetKey(key string) {
   214  	if op == nil {
   215  		return
   216  	}
   217  	op.key = key
   218  }
   219  
   220  func (op loggerTest) Key() string {
   221  	return op.key
   222  }