简体   繁体   English

如何正确保存您的 println 调试?

[英]How to save your println debugs properly?

I normally spend a lot of time println debugging my programs to make sure they do what I believe.我通常会花很多时间 println 调试我的程序,以确保它们按照我的想法运行。 However, when I am done with the debugging there are a lot of unnecessary println's everywhere.但是,当我完成调试时,到处都有很多不必要的 println。 Is there some way of not throwing this work away and still have it for later?有没有办法不把这项工作扔掉,以后还留着? I know that you could write unit test for this and stick the "println's" there, but I would rather not set up a framework for every little program I write.我知道您可以为此编写单元测试并将“println”粘贴在那里,但我宁愿不为我编写的每个小程序建立一个框架。

My suggestion would be to have a debug file and a clean file and just save both.我的建议是有一个调试文件和一个干净的文件,然后保存两者。 Does anyone else have a nice idea for this?还有其他人对此有一个好主意吗? As an example, here is my almost finished scala program for solving the "Sherlock and the Valid String" on Hackerrank see here .例如,这是我几乎完成的 scala 程序,用于解决 Hackerrank 上的“Sherlock 和有效字符串”, 请参见此处

import java.io.PrintWriter
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.{Set => MutSet}
import scala.collection.mutable.{Map => MutableMap}
import scala.collection.mutable.{ListBuffer}


object Solution {

    /**
        @TODO  "Sherlock and the Valid String", Strings
    */


    // Complete the isValid function below.
    def isValid(s: String): String = {
        // Count frequencies
        var freqmap = Map[Char, Int]()
        var old = 0
        for (c <- s) {
            old = freqmap.getOrElse(c, 0) + 1
            freqmap = freqmap + (c -> old)
        }
        println(s"Counts of characters in string:\n" + freqmap.mkString(", \n"))

        // Check if problems or okay
        var oneDeviation = false
        var initFreq = freqmap(s(0))
        val allowedFreqs = ListBuffer(initFreq)
        val counter = MutableMap[Int, Int](initFreq -> 1)
        var isFreqOne = false
        var freqThatIsOne: FreqIsOne = None

        println(s"Init freq: $initFreq")
        println(s"counter: " + counter.mkString(", \n"))
        println()
        for (freq <- freqmap.values) {
            println("------------------")
            println(s"Freq: $freq")

            if (!oneDeviation) {
                if (freq != initFreq) {
                    oneDeviation = true
                    // greater than 1 diff and no freq is 1 , eg 2 and 4
                    // if pass this we know diff is 1
                    if (isBiggerThanOneDiffAndNoOneFreq(initFreq, freq)) {
                        return "NO"
                    }
                    // check if we have a freq that is one
                    if (initFreq == 1 || freq == 1) {
                        // one freq which is 1
                        if (initFreq == 1) {
                            isFreqOne = true
                            freqThatIsOne = Init
                            // only freq allowed
                            allowedFreqs.remove(0)
                            allowedFreqs += freq
                        } else if (freq == 1) {
                            isFreqOne = true
                            freqThatIsOne = Freq
                            // only allowed is initFreq
                        }
                    // one diff but larger than 1 freqs, only allowed
                    // is the lower frequency
                    } else {
                        if (freq > initFreq) {
                            allowedFreqs.remove(0)
                            allowedFreqs += freq
                        }
                    }

                // no new freq
                } else {

                }
            } else {
                if (!allowedFreqs.contains(freq)) return "NO"
            }
            old = counter(initFreq)
            counter(initFreq) = old + 1
            println(s"counter: " + counter.mkString(", "))
        }
        "YES"
    }

    def isBiggerThanOneDiffAndNoOneFreq(initFreq: Int, freq: Int): Boolean = {
        math.abs(freq - initFreq) > 1 && (initFreq != 1 && freq != 1)
    }

    sealed trait FreqIsOne
    case object Init extends FreqIsOne
    case object Freq extends FreqIsOne
    case object None extends FreqIsOne

    def main(args: Array[String]) {
        val stdin = scala.io.StdIn


        val s = stdin.readLine

        val result = isValid(s)
        println(result)

    }
}

Use a logging framework that allows control over log level for each component (eg scala-logging ) and leave the debugging code there in case you need it again.使用允许控制每个组件的日志级别的日志框架(例如scala-logging )并将调试代码留在那里以防您再次需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM