Programing

iPhone에서 Objective-C로 메소드 이름 NSLog

lottogame 2020. 6. 13. 10:14
반응형

iPhone에서 Objective-C로 메소드 이름 NSLog


현재, 우리는 클래스 이름과 로그의 소스 라인 번호를 출력하는 확장 로그 메커니즘을 정의하고 있습니다.

#define NCLog(s, ...) NSLog(@"<%@:%d> %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], \
    __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__])

예를 들어, NCLog (@ "Hello world"); 출력은 다음과 같습니다.

<ApplicationDelegate:10>Hello world

이제 메소드 이름을 다음과 같이 로그 아웃하고 싶습니다.

<ApplicationDelegate:applicationDidFinishLaunching:10>Hello world

따라서 어떤 메소드가 호출되는지 알 수 있으면 디버깅이 쉬워집니다. 우리는 Xcode 디버거도 가지고 있다는 것을 알고 있지만 때로는 로그 아웃하여 디버깅하고 싶습니다.


print(__FUNCTION__) // Swift
NSLog(@"%@", NSStringFromSelector(_cmd)); // Objective-C

스위프트 3 이상

print(#function)

기술적으로 질문에 대답하려면 다음을 원합니다.

NSLog(@"<%@:%@:%d>", NSStringFromClass([self class]), NSStringFromSelector(_cmd), __LINE__);

또는 다음을 수행 할 수도 있습니다.

NSLog(@"%s", __PRETTY_FUNCTION__);

tl; dr

NSLog( @"ERROR %@ METHOD %s:%d ", @"DescriptionGoesHere", __func__, __LINE__ );

세부

Apple은 기술 Q & A 페이지를 보유하고 있습니다 . QA1669-현재 방법 또는 라인 번호와 같은 컨텍스트 정보를 로깅 내역에 어떻게 추가 할 수 있습니까?

로깅을 지원하려면 다음을 수행하십시오.

  • C 전처리 기는 몇 가지 매크로를 제공 합니다 .
  • Objective-C는 표현식 (방법)을 제공합니다 .
    • 패스 암시 인수 현재 방법의 선택를 들어 :_cmd

다른 답변에서 알 수 있듯이 현재 메소드의 이름을 얻으려면 다음을 호출하십시오.

NSStringFromSelector(_cmd)

현재 메소드 이름 얻으려면 하고 , 현재의 행 번호를 두 매크로를 사용 __func__하고 __LINE__여기에서 볼 수 있듯이 :

NSLog(@"%s:%d someObject=%@", __func__, __LINE__, someObject);

또 다른 예 ... Xcode의 코드 스 니펫 라이브러리에 보관하는 코드 스 니펫 :

NSLog( @"ERROR %@ METHOD %s:%d ", @"DescriptionGoesHere", __func__, __LINE__ );

… 오류 대신 TRACE…

NSLog( @"TRACE %@ METHOD %s:%d ", @"DescriptionGoesHere", __func__, __LINE__ );

… 소프트 코딩 된 설명을 사용하여 더 긴 설명 (값 [rows count])을 전달합니다 .

NSLog( @"TRACE %@ METHOD %s:%d.", [NSString stringWithFormat:@"'Table of Contents.txt' file's count of Linefeed-delimited rows: %u.", [rows count]] , __func__, __LINE__ );

로깅을위한 전 처리기 매크로

매크로의 양쪽에 쌍의 밑줄 문자를 사용하십시오.

| 매크로 | 형식 | 기술
  __func__ % s 현재 함수 서명
  __LINE__ % d 현재 줄 번호
  __FILE__ % s 소스 파일의 전체 경로
  __PRETTY_FUNCTION__ % s __func__와 유사하지만 자세한 정보 포함
                                    C ++ 코드의 유형 정보. 

로깅 표현식

| 표현 | 형식 | 기술
  NSStringFromSelector (_cmd) % @ 현재 선택기의 이름
  NSStringFromClass ([self class]) % @ 현재 객체의 클래스 이름
  [[NSString % @ 소스 코드 파일 이름
    stringWithUTF8String : __ FILE__]   
    lastPathComponent] 
  [NSThread callStackSymbols] % @ 스택 추적의 NSArray

로깅 프레임 워크

Some logging frameworks may help with getting current method or line number as well. I'm not sure, as I've used a great logging framework in Java (SLF4J + LogBack) but not Cocoa.

See this question for links to various Cocoa logging frameworks.

Name of Selector

If you have a Selector variable (a SEL), you can print its method name ("message") in either of two ways as described by this Codec blog post:

  • Using Objective-C call to NSStringFromSelector:
    NSLog(@"%@", NSStringFromSelector(selector) );
  • Using straight C:
    NSLog(@"%s", selector );

This information drawn from the linked Apple doc page as of 2013-07-19. That page had been last updated 2011-10-04.


NSLog(@"%@", NSStringFromSelector(_cmd)); // Objective-C
print(__FUNCTION__) // Swift

It's actually just as simple as:

printf(_cmd);

For some reason iOS allows _cmd to be passed as a literal char with not even a compile warning. Who knows


In Swift 4:

func test(){

print(#function)

}

test() //print the value "test()"

참고URL : https://stackoverflow.com/questions/2770307/nslog-the-method-name-with-objective-c-in-iphone

반응형