After most of the past 20 years here in Asia -- the first 7 of them married to a native of Beijing (where we met and married) -- I keep trying to tell myself that terms such as gaijin, gweilo, yanguize (the Mandarin equivalent of gweilo), and farang are harmless references to people not from Japan, China, or Thailand.
But I don't buy it, instead finding the use of such terms offensive, though I find both farang and gaijin far less offensive than either the Cantonese or Mandarin terms.
Yanguize means "foreign devil," period. Mandarin speakers will try to tell you it means foreigner -- but it doesn't; the term for foreigner in Mandarin is weiguoren -- "outside country person," similar to the Japanese. It's 3 characters, and if you look them up in a Chinese English dictionary, that's exactly what the 3 characters mean, with explication. That is, they translate, roughly, as something like "across-the-ocean-person," but historically to be such a person is to be a barbarian, and to be a barbarian is to be a devil, and since, by definition, such devils are foreign, they are foreign devils.
Yes, gweilo has come to mean ghost person in Cantonese. But when I last lived in the mainland I lived in Guandong province, and while I never learned to speak more than a few words of the dialect, Chinese who befriended me eventually admitted that while the commonly accepted notion amongst Chinese for the meaning was ghost person, it is a term of [fearful] disrespect, the fearful part being tied into the notion of one's being a ghost.
As one who taught English and business communications in Asia many years, I am well aware of the significance of tone of voice, body language, social context, etc. to the precise meaning of terms. To use an English example, it's one thing for me to see an fellow native speaker of English with whom I am close friends but whom I've not seen for a long time to say something like, "Hey, you old S.O.B., I haven't seen you in a month of Sundays!" with a slap on the back and a smile than it is for me to say it with my arms folded, legs spread, a frown on my face, and a menacing tone in my voice. And I accept the same applies to all the terms under discussion in this thread.
In the case of Chinese, regardless of dialect, the true underlying attitude is revealed in other ways. Though my Mandarin is limited, I can go into a Chinese restaurant in the U.S. (my original homeland) and, if the waiter's English is even worse than my Mandarin (which takes some doing! and I successfully speak to him in Mandarin, it irks me no end for him to turn to another employee and exclaim "He speaks the national language" or "He speaks the common language!" [the latter being the Mandarin version of the former]. Of course I speak what is the de facto national language of the U.S. -- English. (Okay, in deference to my numerious friend who are Citizens of Empire, I'll say I speak American!

) In any case, the "national language" of the U.S. is most assuredly *not* any Chinese dialect. "National language" meaning "Chinese" applies only in places such as the mainland, including Hongkong and Macau, and Taiwan. And I'm not the least bit shy, in the U.S., of pointing this fact out to the native speaker of Chinese.
Finally, some of my friends from various Asian countries, but especially China, have been stumped when I've asked them just what term I can use to refer to them by the regional, national, or ethnic identity. Using people of Chinese extraction as one example, some have objected even to "Chinese," Asian," Oriental," or the pretty much obselete "Asiatic." That leaves the obviously objectionable terms such as "Chink" and "coolie." I'm reduced to saying ". . . my friend from Beijing" or some such. Yet they can at least call me an American or a foreigner or a Westerner without offending me; why insist on calling me something derogatory? If they don't know me, they have no basis for the implied judgement; if they do know me, they know I object.
The comment has been made that sometimes we are too uptight, and I have to agree with that. One of my best Thai friends sometimes sees me and booms out, "Hey, farang, how are you?" and I know he is simply showing his affection and his knowledge that our friendship of many years' standing is strong enough to make such a greeting entirely appropriate and acceptable. But he's a *close* friend, not just a casual acquaintance I see every now and then nor a stranger. I have thick eyeglasses and would take offense from someone of my own ethnic group and nationality, even local identity, were that person to say, "Hey, four eyes!" unless that person was just as close a friend as my Thai running mate.
At this late date, I don't expect I'll ever believe either local excuses or foreign justifications. But, hey, that's only one person's take, and everyone has to choose his own way. . . .